Node.js Developer interview question

How do you handle errors in Node.js async code?

Await promises inside try/catch at the boundary that can recover, propagate rejections otherwise, attach one terminal handler to background work, and centralize HTTP error translation without swallowing causes or emitting multiple responses.

Direct answer

What a strong answer covers

How it works

Errors in async code travel as rejected promises, and a rejection nobody awaits or catches becomes an unhandledRejection that crashes modern Node by default. The rule is that every promise ends in an await inside try/catch, a .catch, or is returned to a caller that does one of those. Recovery happens at the layer that can act: a request handler translates to an HTTP status once, a job runner decides to retry, a background task logs and reports. Wrapping preserves the original as cause. Callbacks from event emitters and streams need their own error listeners because they do not become promises on their own.

Example

An Express handler awaits the service call inside a helper that forwards rejections to next(err), and one error middleware maps a NotFoundError to 404 and everything else to 500 with a request ID, logging once. A fire-and-forget cache refresh is written as refresh().catch(report) so a failure is recorded instead of crashing the process.

Common mistakes

  • Forgetting to await a promise inside try/catch so the catch never runs.
  • Catching an error, logging it, and sending two responses when the handler continues.
  • Attaching no error listener to a stream and crashing on the first EPIPE.

A useful answer structure

Answer from your own work.

Do not memorise a script: use only examples you can explain honestly in follow-up questions.

Practise this answer with MiraHint

Open the desktop workspace and rehearse a clear answer in your own words.

Download MiraHint
Node.js Developer interview questions