我模拟了 3 个承诺 - 两个已解决,一个被拒绝,包装了一个 try-catch 函数,但我仍然在控制台中收到警告:(节点:4159)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。
const emulate = (id, ms) =>
new Promise((resolve, reject) => {
if (ms > 1500) reject(id);
setTimeout(() => {
resolve(id);
}, ms);
});
const promises = [
emulate(1, 500),
emulate(2, 1000),
emulate(3, 2000)
];
async function stepByStepResolve() {
try {
for await (const promise of promises)
console.log(promise);
} catch (e) {
console.log('Err:', e);
}
}
stepByStepResolve();