0

My function keeps executing indefinitely, although I would have expected it to resume after 10ms. Is there any way I can flush timeouts while I am waiting for the result? Or at least tell JS to execute / resolve other promises before I return my result?

The simple matter is, I NEED the response before I can continue execution. And I can not put my code in an async block whatsoever. I also can not use 'then' since I need to return the raw result. I thought this hack would work, but nope.

As far as my understanding of JS goes, this simply is not possible, but I would love to know.

let test = function () {
    let wait = true;

    setTimeout(() => {
        wait = false
    }, 10);

    while (wait);

    return wait;
}
Harm Smits
  • 437
  • 3
  • 10
  • 1
    (There's no promise in the example, but it's still async) – evolutionxbox Jun 01 '21 at 12:07
  • 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) – evolutionxbox Jun 01 '21 at 12:07
  • 3
    "_this simply is not possible_". You got it right. – Ivar Jun 01 '21 at 12:08
  • What you need is a tutorial of the asynchronous concept and promises. – Teemu Jun 01 '21 at 12:08
  • @evolutionxbox Thanks it's nice explained in that post. Nice question. – Vivek Bani Jun 01 '21 at 12:10
  • @Teemu I am perfectly aware of asynchronous concepts. I even wrote my own libuv for certain school projects. I am just asking whether the concept I provided, is even possible in JS. I just faced the cold hard truth, that a promise with callables does NOT do what I need it to do. I *NEED* to wait for a result, I can not use a callback since I need to *STOP* execution. – Harm Smits Jun 01 '21 at 12:10
  • 1
    Your code doesn't show that awareness ... – Teemu Jun 01 '21 at 12:11
  • @evolutionxbox Nope, I thought setTimeout would break execution when busy waiting, but sadly that is not the case. Just sad there is no way to halt execution on a top level really. – Harm Smits Jun 01 '21 at 12:13
  • @codam_hsmits JS uses promises to execute code at a later time. Doesn't that result in the same output? – evolutionxbox Jun 01 '21 at 12:14
  • 3
    "And I can not put my code in an async block whatsoever" Why not? `async` and `await` do exactly what you are asking for. – DBS Jun 01 '21 at 12:17
  • @codam_hsmits If you have control over your runtime (like access to node source code), you can of course build such a native "flush event loop" function, but it's not part of normal js (and certainly not available on the browser for example). On node.js, you might want to have a look at the "fiber" library which does something like this. – Bergi Jun 01 '21 at 12:21
  • @Bergi Yep that does seem to be the case. I wanted to basically suspend the 'top level' fiber if you will. But it doesn't seem to be possible. So I will have to find a way around this, I can already tell I will have a great week )) – Harm Smits Jun 01 '21 at 12:22
  • 1
    @codam_hsmits If you told us more about your requirements, we could give more specific advice. What runtime? What constraints? Why can't you use async? – Bergi Jun 01 '21 at 12:23
  • 2
    "*The simple matter is, I NEED the response before I can continue execution. And I can not put my code in an async block whatsoever. I also can not use 'then' since I need to return the raw result.*" I very much doubt any of this is true. – VLAZ Jun 01 '21 at 12:24
  • Its a remote procedure viewer if you will. But the fact is, the project is written non-reactive and has ~270k lines of code. Rewriting is not really an option, hence the issues. It seems I will have to start juggling with proxy objects that I pre-format and then force to recompute a lot of my results. I guess I know what I will be doing for the next few years :/ – Harm Smits Jun 01 '21 at 12:31
  • @Bergi It is pretty hard to explain a project of such magnitude in only a few lines. Regardless, the answer simply is, 'its not possible'. – Harm Smits Jun 01 '21 at 12:36
  • In the browser, you can also use synchronous ajax still… For node I would recommend trying node-fibers. Both options should be pretty non-intrusive (require few changes) even in a large codebase. Sure, they might not perform as well as proper asynchronous code, but they'd work. Make sure to get professional advice (not just some random guys on the internet) if you bet the next few years of coding on this. – Bergi Jun 01 '21 at 13:26
  • @Bergi No, don't worry, its just a few minor fixups. A rework is scheduled in a few years from now. Considering this issue, I will try to move it forward but for now, I don't intend to rewrite everything to promises. – Harm Smits Jun 01 '21 at 13:36

1 Answers1

0

you can use async/await , to block the flow of the function

example

const mine = async() => {
console.log("start")
await anotherAsyncFunction()
console.log("done")
}
Amir Saadallah
  • 668
  • 1
  • 8
  • 19
  • 1
    I *specifically* want to suspend the main thread and await a result. Async/await does not do this. – Harm Smits Jun 01 '21 at 12:23
  • still, the same idea, if all your logic comes after the await it will be suspended until the await finish the work – Amir Saadallah Jun 01 '21 at 12:34
  • 1
    No. I am looking for a similar functionality to '(u)sleep' in c / c++. Where the scheduler will temporarily suspend your *ENTIRE* process, and allow other process to continue running. Now, you could have done this, if you could 'flush' the callables by using suspending the top level fiber, but you can't in javascript, so its literally impossible. – Harm Smits Jun 01 '21 at 12:38