0

I have an XHR-Request returning an OAuth2 Token. I want my code to wait until i received that token (or received 401 or something like that) and then go on. Sadly i found out that there is nothing like "Thread.sleep" in JavaScript. What is the best way to wait for XHR-Requests to finish before going on with the code?

My code uses the onreadystatechange-function of the XHR-Object (so the other code will go on while the request is still executing). My method is invoked and should return the token, so i can't just go with the

if ((status===200) && (readyState===4))

condition.

Rüdiger
  • 893
  • 5
  • 27
  • 56
  • Well your code needs to learn how to work with an asynchronous call..... Plenty of examples on how to use promises and callbacks.... https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – epascarello Aug 09 '17 at 13:41
  • You can start with this: [Using promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) – Nitzan Tomer Aug 09 '17 at 13:43

0 Answers0