'Executes asynchronous JavaScript code.
Similar to `Execute Javascript` except that scripts executed with
this keyword must explicitly signal they are finished by invoking the
provided callback. This callback is always injected into the executed
function as the last argument.
Scripts must complete within the script timeout or this keyword will
fail. See the `Timeouts` section for more information.
Examples:
| Execute Async JavaScript | var callback = arguments[arguments.length - 1]; | window.setTimeout(callback, 2000); |
| Execute Async JavaScript | ${CURDIR}/async_js_to_execute.js                |                                    |
| ${retval}=               | Execute Async JavaScript                        |                                    |
| ...                      | var callback = arguments[arguments.length - 1]; |                                    |
| ...                      | function answer(){callback("text");};           |                                    |
| ...                      | window.setTimeout(answer, 2000);                |                                    |
| Should Be Equal          | ${retval}                                       | text                               |'

@keyword
def execute_async_javascript(self, *code):

    js = self._get_javascript_to_execute(''.join(code))
    self.info(('Executing    Asynchronous    JavaScript:\n%s' % js))
    return self.browser.execute_async_script(js)
