0

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?
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 2
    You're probably barking up the wrong tree. Please describe the situation why you need to wait for `x` to become defined, and we can tell you how to do that idiomatically in Javascript. This pattern seems… odd, to say the least. – deceze Aug 08 '18 at 09:05
  • 2
    Either `some_lib_function` is synchronous then `wait` isn't useful at all. Or `some_lib_function` is asynchronous then there will be a way to "wait" for the response, either by passing a callback function or an event or it returns a Promise or ... – Andreas Aug 08 '18 at 09:05
  • @deceze, Coming from OOP background (C++/Java). I don't want to deal with the callbacks, which makes the code overly complex. Hence, I want to wait, until certain task is finished. There are some external libraries such as [Q](https://github.com/kriskowal/q) and [wait-for](https://github.com/luciotato/waitfor-ES6). But I am too novice to decide, whether it's a right choice. – iammilind Aug 08 '18 at 09:08
  • @Andreas, `some_lib_function` is probably not synchronous. For example, I am using `FileSystem`'s `getFile()` right now. Which has a callback to get a `FileEntry`. Then there is another set of callbacks within that to get `Metadata` and `FileWriter`. BTW, I am in TS environment with Google protobuf in place, so I am not sure, how far my project can support the ES6. However, ideas are welcome. – iammilind Aug 08 '18 at 09:10
  • @deceze, Yes to avoid "C" style coding which is full of callbacks, I am trying to get a C++/Java style "wait()" mechanism. A person from those background, will find "callback" way difficult to understand. In case, "learning JS" is all that you want to say, then I will take it as an advice. But that doesn't answer the question. If you think, it's not possible, then post it as an answer. Thanks. :-) – iammilind Aug 08 '18 at 09:12
  • 1
    In a nutshell, you cannot synchronously await an asynchronous result, because the asynchronous result will never arrive if you hog the single thread with a blocking synchronous function. You *must* yield the thread in order for the asynchronous task to ever finish, and you *must* continue execution of your code later, and the only way to create this interruption is using callbacks or `await`. – deceze Aug 08 '18 at 09:15
  • @iammilind actually JS is based on the concept of "green threads", and all heavy stuff (network / IO / etc.) is done by the engine in seperate threads (out of your control). That means `makeNetworkRequest(function callback() { /*..*/ })` actually runs the heavy stuff in a new thread, and when thats done it shedules the callback to the main thread. Blocking the only thread you can control is somewhat senseless. – Jonas Wilms Aug 08 '18 at 09:57
  • @JonasW., deceze -- Can you check the updated code. There might some tit-bits here, but I am looking for a non-critical performing browser based JS implementation for the `wait()`. I have gone through the duplicate link, but for the sake of completeness, want to know if such implementation is accepteable. – iammilind Aug 08 '18 at 11:28
  • That doesnt make sense at all. Just put the following code *inside* the callback? – Jonas Wilms Aug 08 '18 at 11:30
  • @JonasW., yes I realize that this will also not work. Apparently there is no viable solution, otherwise it would have been found by now already. – iammilind Aug 08 '18 at 11:45
  • *no viable solution* ?! Callbacks! Promises! `async` / `await` ! You are just not willing to accept how Javascript works. – Jonas Wilms Aug 08 '18 at 11:46
  • @JonasW., I am using a "FileSystem"'s `getFile()`, which is not `async`, hence I can't use `await`; otherwise it's my first choice. I want to avoid "Callbacks" for the obvious reasons as stated in the question. In general, I wanted to simulate the `await()` like functionality. If you have any helpful post, which explains how to implement our `Promise` wrappers then it will be useful. Thanks. – iammilind Aug 08 '18 at 11:53
  • To easy, just `const {promisify} = require("util");` then `await promisify(readFile)("that");` – Jonas Wilms Aug 08 '18 at 12:24

0 Answers0