1

I thought the process was stuck waiting for resolve. But it returned "Hello, A" and process exit.

I can't understand why it just exited.

I know it just solve if i just add resolve or reject.

But i want something deep.

function funA(name) {
  return new Promise(function(resolve, reject) {
    console.log("Hello, " + name);
  });
}

(async() => {
  await funA("A");
  await funA("B");
})();
// Hello, A
// exit

VScode Image


Resolve

what happens when a Promise never resolves?

1mm.p
  • 37
  • 5

2 Answers2

2

Make sure to resolve your promise, otherwise it will still wait for FunA("A") to be finished. You can do this by calling resolve():

function funA(name) {
  return new Promise(function (resolve, reject) {
    console.log("Hello, " + name);
    resolve();
  });
}

(async () => {
  await funA("A");
  await funA("B");
})();

EDIT: to answer your question why it exits and is not pending if you don't resolve: this happens when there are no more callbacks to process by nodejs. When you return your broken pending promise without a resolve, you have not created a callback in the nodejs queue, thus nodejs decided to exit

Laurens
  • 2,596
  • 11
  • 21
1

Essentially, your function calls have gone into limbo... They haven't exited...

Even node console shows a message...

Hello, A
Promise { <pending> }

And please also note, it's the first invocation itself, which has gone into limbo.. 2nd one wouldn't even have started... The first "await" itself has not finished execution...which means, though your function looks to have ran to completion, but the "Promise" within which it is executing has not...And that is why the await hasn't finished...

Nalin Ranjan
  • 1,728
  • 2
  • 9
  • 10