I want my code to wait until some variable x is defined properly as demo below:
var x;
call_some_async_function(arg, success_callback);
wait(function () { return x == null || x == undefined; }); // until the `success_callback()` is called
Apparently there is no wait()
function in Javascript/Typescript. However there are await
and async
keywords for the objects which deal with promise.
Coming from C++/Java background, I was wondering if we can implement the wait()
functionality in a reasonable way for non-critical performance.
wait (Continue: () => boolean)
{
setTimeout(function()
{
if(Continue())
wait(Continue);
},
10 /* milliseconds */);
}
Questions:
- Is the above code, correct way to implement
wait()
? (also is it a bad recursion) Assume it for the non-critical browser based systems. - Is there any elegant way or library for the same?