Javascript 转发承诺会导致承诺链-为什么我得到承诺对象而不是拒绝值
我在理解转发承诺结果(嵌套承诺)是如何工作的方面存在问题 这段代码按照我的预期工作(在最后,我得到了整数值):Javascript 转发承诺会导致承诺链-为什么我得到承诺对象而不是拒绝值,javascript,es6-promise,Javascript,Es6 Promise,我在理解转发承诺结果(嵌套承诺)是如何工作的方面存在问题 这段代码按照我的预期工作(在最后,我得到了整数值): 函数optharesolves(){ 返还承诺。决议(1); } 将被向前移动的函数选项(x){ 返回新承诺(功能(解决、拒绝){ 决议(是其他承诺(x)); }) } 函数yetAnotherNestedPromise(x){ 返回承诺。解决(-x); } opthatsolves() .然后(x=>opThatWillBeForwarded(x)) .然后(x=>x*2) .然后
函数optharesolves(){
返还承诺。决议(1);
}
将被向前移动的函数选项(x){
返回新承诺(功能(解决、拒绝){
决议(是其他承诺(x));
})
}
函数yetAnotherNestedPromise(x){
返回承诺。解决(-x);
}
opthatsolves()
.然后(x=>opThatWillBeForwarded(x))
.然后(x=>x*2)
.然后(x=>x*2)
.then(x=>console.log(“已解析:+x))
.catch(x=>console.log(“拒绝:+x))
,因为它首先不应该收到承诺
拒绝
回调应包含失败的原因(即错误
)
请注意,可以通过后续的错误回调来重新返回错误:
new Promise((resolve, reject) => {
reject(new Error("failed !"));
})
.then(null, reason => {
console.log(1, reason);
throw reason;
})
.catch(reason => {
console.log(2, reason);
});
new Promise((resolve, reject) => {
reject(new Error("failed !"));
})
.then(null, reason => {
console.log(1, reason);
})
.then(result => {
console.log("Success! Let's throw something else...");
throw new Error("Another failure");
})
.catch(reason => {
console.log(2, reason);
});
还请注意,如果错误回调无法重新显示错误(或引发另一个错误),则后续的方法将启动其成功回调:
new Promise((resolve, reject) => {
reject(new Error("failed !"));
})
.then(null, reason => {
console.log(1, reason);
throw reason;
})
.catch(reason => {
console.log(2, reason);
});
new Promise((resolve, reject) => {
reject(new Error("failed !"));
})
.then(null, reason => {
console.log(1, reason);
})
.then(result => {
console.log("Success! Let's throw something else...");
throw new Error("Another failure");
})
.catch(reason => {
console.log(2, reason);
});