Home > Back-end >  Node logging unexpected UnhandledPromiseRejectionWarning
Node logging unexpected UnhandledPromiseRejectionWarning

Time:12-17

I have a piece of code that's causing Node to log UnhandledPromiseRejectionWarning. But I'm not sure why. Here's the code boiled down:

export class Hello {
  async good(): Promise<string> {
    let errorP = this.throwError();
    let responseP = this.doSomething();
    let [error, response] = await Promise.all([errorP, responseP]);
    return response   '123';
  }

  async bad(): Promise<string> {
    let errorP = this.throwError();
    let responseP = this.doSomething();
    let response = (await responseP)   '123';
    let error = await errorP;
    return response;
  }

  private async throwError(): Promise<string> {
    await (new Promise(resolve => setTimeout(resolve, 1000)));
    throw new Error('error');
  }

  private async doSomething(): Promise<string> {
    await (new Promise(resolve => setTimeout(resolve, 1000)));
    return 'something';
  }
}

Calling try { await hello.bad(); } catch (err) {} causes node to log UnhandledPromiseRejectionWarning

Calling try { await hello.good(); } catch (err) {} does NOT log the warning

Full error:

(node:25960) UnhandledPromiseRejectionWarning: Error: error
    at Hello.<anonymous> (C:\hello-service.ts:19:11)
    at Generator.next (<anonymous>)
    at fulfilled (C:\hello-service.ts:5:58)
    at runNextTicks (internal/process/task_queues.js:58:5)
    at listOnTimeout (internal/timers.js:523:9)
    at processTimers (internal/timers.js:497:7)
(node:25960) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()
. To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:25960) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
{"level":30,"time":1639745087914,"pid":25960,"hostname":"AZWP10801-12","reqId":"req-1","dd":{"trace_id":"2081604231398834164","span_id":"2081604231398834164","service":"@amerisave/example-service","version":"0.0.0"},"res":{"statu
sCode":200},"responseTime":1025.4359999895096,"msg":"request completed"}
(node:25960) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)

Some dependency versions: node ver. 14.16.1 ts-node-dev ver. 1.1.8 ts-node ver. 9.1.1 typescript ver. 4.5.2

Why is good good, but bad bad?

CodePudding user response:

Here's a hypothesis (that can be experimentally proven).

The difference in behavior between good and bad can be explained by the order of awaits.

In bad you're awaiting on throwError after you have awaited on doSomething, while in good, you're awaiting on Promise.all, which will not return until both are fullfilled or at least one is rejected (which will be the case here).

So in bad, the throwing is happening outside of await, and your catch is not triggered, and it is caught internally by node.

If you change your bad so that you await on throwError first, then your catch will get triggered:

async bad(): Promise<string> {
    let errorP = this.throwError();
    let responseP = this.doSomething();
    let error = await errorP;
    let response = (await responseP)   '123';
    return response;
  }

CodePudding user response:

The problem in bad() is because the errorP promise rejects BEFORE you get to await errorP and thus it rejects when there is no reject handler for it in place. Nodejs detects that a promise rejected and your process gets back to the event loop and that rejected promise does not have a reject handler on it. That gets the "unhandled rejection" warning.

Nodejs doesn't know you're going to add one in the future with code that will execute some time in the future, so it will report the unhandled rejection.

Code becomes subject to these types of errors where you put a promise into a variable and you have no reject handler on that promise of any kind and then you go await on some other promise BEFORE you ever put any sort of reject handler on that previous promise. The promise is just sitting there with no error handling on it. If, due to the timing of things, it happens to reject in that state, you will get the warning. The usual solutions are:

  1. Immediately put error handling on the promise so it's never left sitting by itself.
  2. Don't create the promise until you're ready to use it however you're going to use it with appropriate error handling (with a .then().catch() or in a Promise.all().catch() or in an await or whatever).
  3. Don't await other promises while a promise is sitting in a variable without any reject handling.

I find that if I can avoid putting a promise with no handling on it into a variable at all and rather just create the promise right into the circumstance where it's going to be monitored for completion and error, you don't even have to generally think about this issue.

  • Related