Я получаю UnhandledPromiseRejectionWarning
, когда я запускаю этот простой код:
var d = new Promise((resolve, reject) => {
if (false) {
resolve('hello world');
} else {
reject('no bueno');
}
});
d.then((data) => console.log('success : ', data));
d.catch((error) => console.error('error : ', error));
Полный ответ:
error : no bueno
(node:12883) UnhandledPromiseRejectionWarning: no bueno
(node:12883) 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(). (rejection id: 2)
(node:12883) [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.
Похоже, d.catch()
запускается.Я заметил, что если закомментировать d.then()
, предупреждающее сообщение исчезнет.
Я вызываю скрипт из терминала, как node foobar.js
.
Я что-то не так делаю?
Протестировано с узлами v8.14, v10 и v11 под MacOS High Sierra.