title: Does Selenium wait for JavaScript to complete?
body: <p>I am testing a JavaScript API using Java/Selenium.</p>
  
  <p>I issue these commands on Java side,</p>
  
  <pre><code>executor.executeScript("setName('Hello');");
  // Thread.sleep(2000);
  String output = (String)executor.executeScript("return getName()");
  assertEquals(output, "Hello");
  </code></pre>
  
  <p>In JavaScript side, this is an async function, so it is supposed to take some time and set the variable.</p>
  
  <pre><code>function setName(msg) {
      // simulate async call 
      setName(function(){name = msg;},1000);
  }
  </code></pre>
  
  <p>I need to wait for this async function to complete before moving to the next line in Java, where the <code>assertEquals()</code> is executed.</p>
  
  <p>Is there any way to accomplish this without using <code>Thread.sleep()</code> on Java side.</p>
  
  <p>Thanks</p>