title: Get a response from Codeception $I->executeAsyncJS
body: <p>I'm trying to execute an AJAX request in the browser in Codeception. There is the function</p>
  
  <pre><code>$I-&gt;executeAsyncJS();
  </code></pre>
  
  <p>Manual <a href="https://codeception.com/docs/modules/WebDriver#executeAsyncJS" rel="nofollow noreferrer">https://codeception.com/docs/modules/WebDriver#executeAsyncJS</a></p>
  
  <p>I'm trying to understand the manual. It says:</p>
  
  <blockquote>
    <p>Executes asynchronous JavaScript. A callback should be executed by
    JavaScript to exit from a script. Callback is passed as a last element
    in arguments array. Additional arguments can be passed as array in
    second parameter.</p>
  </blockquote>
  
  <p>I just don't understand what this is saying. Here's what I have so far:</p>
  
  <p>JS on Page:</p>
  
  <pre><code>const Done = {};
  Done.result = function(){
      return "Happy";
  }
  </code></pre>
  
  <p>Codeception Selenium WebDriver Chrome Test:</p>
  
  <pre><code>&lt;?php
  
  class SEQuestionCest
  {
  
      public function itShouldReturnJoy(\AcceptanceTester $I){
          $I-&gt;amOnPage("/page.php");
  
          $happy = $I-&gt;executeJS("return Done.result();");
          $I-&gt;assertEquals($happy, "Happy"); //This works
          $happy = $I-&gt;executeAsyncJS("setTimeout(function() { return Done.result(); }, 2000);"); //I'm doing something wrong
  
          $I-&gt;assertEquals($happy, "Happy");
      }
  }
  </code></pre>
  
  <p>Result:
  <a href="https://i.stack.imgur.com/T7Ivn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T7Ivn.png" alt="enter image description here"></a></p>
  
  <p>I expected it to return after 2 seconds, but instead it just times out after about 20 seconds. I thought the timeout function would simulate an AJAX call. I'm doing something very wrong obviously.</p>