【发布时间】:2019-05-10 23:48:02
【问题描述】:
我无法弄清楚如何将嵌套的 Promise 链分解为主 Promise 链。这是我的代码:
//Main Promise chain
let promiseMain = Promise.resolve(1)
.then(result => functionA())
.then(result => nestedChain()).catch((error) => {
console.log(error);
})
.then(result => functionC())
//chain error handler
function chainError(err) {
return Promise.reject(err)
};
function nestedChain()
{
stepOne()
.then(stepTwo, chainError)
.then(stepThreee, chainError)
.catch((error) =>
{
console.log(error);
return undefined;
});
}
function stepOne()
{
return chainError("error attempt : 1.00");
}
一旦我进入我在 stepOne() 中抛出错误的嵌套链,我就能够打破这个嵌套链。惊人的!
问题:它还破坏了主要的承诺链。所以.. 当它进入nestedChain() 并且从stepOne() 抛出错误时,主promise 链中的functionC 将永远不会被执行,因为来自nestedChain 的被拒绝的promise 也会破坏这条链。
【问题讨论】:
-
Promises 不会抛出 - 您正在考虑async/await所表现出的行为 -
这解释了我的 try/catch 失败,谢谢!但是仍然..是否可以仅在嵌套链中中断?
标签: node.js typescript promise