Trung BìnhNode.js iconNode.js

Cách xử lý unhandled Promise rejections trong Node.js?

Node.js 14 trở về trước: unhandled rejection chỉ print warning, process tiếp tục — silent failure nguy hiểm.

Node.js 15+: mặc định crash process với exit code 1 — breaking change.

Hierarchy xử lý đúng:

  1. try/catch trong async functions là chính,
  2. .catch() chain cho fire-and-forget promises,
  3. process.on('unhandledRejection', (reason) => { logger.error('Unhandled rejection', reason); gracefulShutdown(1); }) là safety net cuối — không phải cơ chế chính. process.on('uncaughtException', (err) => { logger.error(err); gracefulShutdown(1); }) cho sync throws

Monitoring: gửi đến Sentry trước khi shutdown — Sentry.captureException(reason); await Sentry.flush(2000).

Graceful shutdown: stop accepting requests, wait for in-flight, close DB, exit.

Dùng ESLint rule @typescript-eslint/no-floating-promises để catch missing awaits lúc compile time — tốt hơn runtime detection.

Xem toàn bộ Node.js cùng filter theo level & chủ đề con.

Mở danh sách Node.js