我正在重构一些代码并删除不必要的回调。
原始代码是:
create(_job_id).then(function (create_id) {
latest().then(function (latest_config) {
update(create_id).then(function () {
// More code
}).catch(function (err) {
console.log(err);
});
.catch(function (err) {
console.log(err);
});
.catch(function (err) {
console.log(err);
});
我将其重构为:
const setup = async () => {
const create_id = create(_job_id);
const latest_config = await latest();
await update(create_id);
return { create_id, latest_config };
}
setup().then((setup) => {
console.log(setup);
})
但是现在拒绝的情况没有得到处理。我将如何优雅地处理“捕获”。 Promises.all
看起来不错,但我尝试使用它但没有成功。
感谢任何帮助。
最佳答案
非异步
版本也可能如下所示:
const p_latest_config = latest();
create(_job_id)
.then(create_id => {
update(create_id).catch(err => console.log(err));
return Promise.all([create_id, p_latest_config]);
})
.then(([create_id, latest_config]) => ({create_id, latest_config}))
.then(setup => console.log(setup))
.catch(err => console.log(err));
上面假设 create(_job_id)
、latest()
和 update(create_id)
不通过内部突变相互依赖.
关于javascript - 如何捕获多个功能的拒绝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59734020/