title: how to make Javascript setTimeout returns value in a function
body: <p>I have a function that calls some service and returns the response. If the response is FALSE, it waits 1 second to ask the service again (which then probably returns TRUE).</p>
  
  <p>How can I do to call my function "checkService()" once, and get the real value? (the first or second try, decided by the function) I set the RET value inside the function, but the functions always return the first RET, because the setTimeout is asynchronous. </p>
  
  <p>In other words, I need some "sleep" trick, or any solution (may be jQuery too).</p>
  
  <pre><code>function checkService() {
  
    //this may return TRUE or FALSE
    var RET = someServiceResponse(); 
  
    // here waits 1 second, then ask the service again
    if( RET == true ) {
      return true;
    } else {
  
      setTimeout(
          function() {
              //it must return the second response of the service
              RET = someServiceResponse();  
          }, 
          1000
      );
  
      // I want the checkService() return the response after de timeout
      return RET;  
    }
  }
  
  function alertResponse() {
      alert( checkService() );
  }
  </code></pre>