【发布时间】:2018-04-07 10:31:31
【问题描述】:
我想要这样的东西:
let promises = [];
for (let i = 0; i < some_length; i++) {
let output = '';
promises.push(some_func(i)
.then((result) => {
output = result;
})
.catch((error) => {
output = error.message;
})
.finally(() => {
console.log(output);
})
);
}
return Promise.all(promises);
但我收到运行时错误.then(...).catch(...).finally is not a function。
我该如何解决这个问题?
【问题讨论】:
-
finally尚不在标准中,因此 Node.js 不支持。不过你可以使用this polyfill -
@Bergi:好的,换句话说,切换到(更新的)
async/await范式,无论如何我们都计划在某个时候这样做,但希望会有在Pomise范式中是一个类似的机制。谢谢!!! -
@goodvibration
async/await仍在使用 Promise 范式,只是语法糖更多。 -
顺便说一句,对于您问题中的示例,我建议您简化为
some_func(i).catch(err => err.message).then(console.log)(这可能比任何finally+await更好,因为您总是需要output变量)
标签: node.js promise try-catch-finally