0

I am working in node.js v18.2.0 and here is my code:

async function res_asyncf(){
  await setTimeout(r => {}, 1000);
}
const res_promise = new Promise(async r => {
  await setTimeout(r, 1000);
});

async function not_res_asyncf(){
  while(true){ }
}
const not_res_promise = new Promise(async r => { });

(async () => {

  console.log("Async wrapper entered");
  await <async_thing_here>;
  console.log("Promise resolved");

})();

Instead of <async_thing_here> I was writing res_asyncf(), res_promise, not_res_asyncf() and finally not_res_promise.

The first one did not wait, which I do not understand.

The second behaved as expected: it hung for a second and then Promise resolved was printed.

The third one also behaved as expected: it hung forever.

But the last one just did nothing and exited and even did not print Promise resolved. Expected: it hangs just as the third one.

Why does all this happen?

gXLg
  • 184
  • 13
  • 3
    You have so much unessecary stuff in here. It does not get printed because you never resolve it. Its still in an pendinge state. – bill.gates Jun 02 '22 at 10:39
  • The print statement is after the promise. If it not resolving I expect to hang too, but it just exits – gXLg Jun 02 '22 at 10:41
  • 1
    `The first two behaved as expected: they hung for a second and then Promise resolved was printed.` the first one does not wait 1 sec. – bill.gates Jun 02 '22 at 10:42
  • Yeah, I edited the question with more specifications – gXLg Jun 02 '22 at 10:47
  • `setTimeout(r => {}, 1000)` is not a promise(the dude who answered said it below too) – The Bomb Squad Jun 02 '22 at 10:50
  • 1
    Are you planning to change your question more? Or is this it? – trincot Jun 02 '22 at 10:54
  • Actually this is it already, the answer from bill.gates did not help me at the moment – gXLg Jun 02 '22 at 10:55
  • 2
    Worth reading [Process exits with unresolved promises](https://github.com/nodejs/promises-debugging/issues/16) and [script ends despite unresolved promise](https://stackoverflow.com/questions/63918440/script-ends-despite-unresolved-promise). – jarmod Jun 02 '22 at 11:27

1 Answers1

1

There still confusion about async / await. They put await infront of everything and expect it to be awaited somehow magically, but thats not how it works.

await makes ONLY sense to use it on Promises. setTimeout does not return an promise, so its nonsense to use it on it.

If you want to use async / await then just ask 2 Questions:

  1. Does this function, method etc. returns an promises?

    1.1 If yes, you could use async / await

    1.2 If no, dont use it. It probably makes no sense.

In your case you have setTimeout:

  1. Does it return an Promise?

Well, you might say, i dont know.

But you can google it: https://developer.mozilla.org/en-US/docs/Web/API/setTimeout

The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout()

Now we get to

1.2 If no, dont use it. It probably makes no sense.

Let me rewrite it:

const res_promise = new Promise(r => {
   setTimeout(r, 1000);
});

function not_res_asyncf(){
  while(true){ }
}
const not_res_promise = new Promise(r => {  });

(async () => {

  console.log("Async wrapper entered");
  await not_res_promise;
  console.log("Promise resolved");

})();

The first function is nonsense. You can technically use await on setTimeout it wont throw you an error, but in praxis it just does nothing.

The second function does need async / await at all.

The third also does not need async / await and its blocking the main thread.

The last one does also need async. It does nothing because it never gets resolved or rejected. Its in an pending state.

Differenz between your third and last function is:

Your third function blocks your entire main thread and makes your application unresponseable.

Your last function returns an promise in an pending state wich does not block your main thread and your application is still responseable.

bill.gates
  • 14,145
  • 3
  • 19
  • 47
  • The question was about async/await and promises, why would you rewrite the code? – gXLg Jun 02 '22 at 10:56
  • The setTimeout was dumb from me, so yeah +1 on that. But the rest is just what my question is about – gXLg Jun 02 '22 at 10:57
  • `Your last function returns an promise in an pending state wich does not block your main thread and your application is still responseable.` - why does it exit though and does not wait infinitely for resolving? – gXLg Jun 02 '22 at 10:59
  • @gXLg how do you mean it exits? If i try your code it prints `Async wrapper entered` and waits. Or what do you exactly mean by it exits i dont get it – bill.gates Jun 02 '22 at 11:02
  • The thing is, it does not wait, it exits the program – gXLg Jun 02 '22 at 11:02
  • @gXLg i tested it on node.js. And indeed it exits. I cannot tell why it exits. My guess is that Node.js somehow detects that this pending promise is infinite and so it just exits. – bill.gates Jun 02 '22 at 11:06
  • 2
    @gXLg there is already an answer for this why your programm exits: https://stackoverflow.com/questions/46966890/what-happens-when-a-promise-never-resolves – bill.gates Jun 02 '22 at 11:09