#999 | 2022-04-13 19:25:42

https://developer.mozilla.org/zh-CN/docs/Web/API/Window/unhandledrejection_event

当Promise 被 reject 且没有 reject 处理器的时候,会触发 unhandledrejection 事件;

window.addEventListener('unhandledrejection', function (event) {
  // ...您的代码可以处理未处理的拒绝...
  // 防止默认处理(例如将错误输出到控制台)
  event.preventDefault();
});

nodejs:

import process from 'process';

process.on('unhandledRejection', (reason, promise) => {
  console.log('Unhandled Rejection at:', promise, 'reason:', reason);
  // Application specific logging, throwing an error, or other logic here
});

somePromise.then((res) => {
  return reportToUser(JSON.pasre(res)); // Note the typo (`pasre`)
}); // No `.catch()` or `.then()`