Код: Выделить всё
const fn2 = async (delay) => {
return new Promise(resolve => setTimeout(() => { throw new Error("error") }, delay));
}
try {
await fn2(1000);
} catch (err) {
console.log("Rejected", err);
}
console.log("Finished.");
< /code>
Обратите внимание, что я запускаю это как модуль (.mjs). < /p>
Единственный вывод, который я получаю (после 1000 мс) - ошибка: < /p>
Ошибка
at timeout._ontimeout (///helloworld. (Узел: внутренние/таймеры: 581: 17)
at process.processtimers (Узел: внутренние/таймеры: 519: 7)
Если я жду обещания, почему я не сможет, если я не выношу, код. .catch Код: Выделить всё
fn2(1000).then(result => {
console.log("Success", result);
})
.catch(error => {
console.log("Rejected", error);
})
при timeout._ontimeout (файл: ///helloworld.mjs: 2: 60)
at in lexontimeout (Узел: внутренние/таймеры: 581: 17)
at process.processtimer /> < /blockquote>
Это, кажется, является нюансом settimeout < /code> Поскольку, если я изменю код, ошибка обрабатывается должным образом. < /p>
Код: Выделить всё
const fn2 = async (delay) => {
// return new Promise(resolve => setTimeout(() => { throw new Error("error") }, delay));
return new Promise(resolve => { throw new Error("error") });
}
Ошибка отклоненной: ошибка
at file: ///helloworld.mjs: 3: 41
at new Promise ()
at fn2 (file: //helloworld.mjs: 3: 10) Файл: ///helloworld.mjs: 7: 9
at modulejob.run (узел: внутренний/модули/esm/module_job: 234: 25)
at atync moduleloader.import (узел: внутренний/модули/esm/loader: 473: 24)
atync -asyncruncruneruntryentrypointwointwointwointwointwointwointwointwointwointwointwointwointwointwip (Узел: внутренний/модули/run_main: 123: 5) завершен.
Подробнее здесь: https://stackoverflow.com/questions/796 ... y-catch-bl
Мобильная версия