0

I'm listening to an event from a 3th party control, pre moving from one stage to another (process flow). Within that method you are able to block the progress, preventing the user to move to the next stage. However, if I subscribe to this event with an async method, that blocking method does not have any effect (which is logical)

Unfortunately I need to call an api (fetching data) that only support async calls. This is to check if the user is able to progress or not. So I need to be able to either let the main execution of my method 'pauze' during the async method call or able to call it in a sync way.

I tried with a boolean that is set to false in the .then condition of the promise like this :

let runningAsync = true
AsyncMethod.then(function() {runningAsync = false})
while (runningAsync)
{
}

This does stop my execution on my sync method BUT within my AsyncMethod I have the call to that async API. And for some reason it just hangs on that call as if it's unable to complete (probably due to the while loop somewhere).

Been breaking my head on this but can't find a proper solution for this issue. Any suggestion would be much appreciated ...

Regards, Sven

  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – VLAZ Jul 01 '22 at 10:01
  • `await` it. You cannot synchronize asynchronous code. – tkausl Jul 01 '22 at 10:01
  • JavaScript has a single event loop. When the promise in `AsyncMethod` resolves, it will be put on a queue to be dealt with when the event loop is free. Your `while` loop is keeping it too busy to check. – Quentin Jul 01 '22 at 10:02
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Quentin Jul 01 '22 at 10:03

0 Answers0