私は、約束の結果(ネスティングの約束)をどのようにフォワードするかを理解することに問題があります。プロミスチェーンの結果を転送する - 拒否された値の代わりにプロミスオブジェクトを取得する理由
このコードは(最終的に、私は整数値を取得します)私は期待どおりに動作:
function opThatResolves() {
return Promise.resolve(1);
}
function opThatWillBeForwarded(x) {
return new Promise(function(resolve, reject) {
resolve(yetAnotherNestedPromise(x));
})
}
function yetAnotherNestedPromise(x) {
return Promise.resolve(-x);
}
opThatResolves()
.then(x => opThatWillBeForwarded(x))
.then(x => x * 2)
.then(x => x * 2)
.then(x => console.log("Resolved: " + x))
.catch(x => console.log("Rejected: " + x))
だから私は、私はreject
にresolve
を変更した場合、私は同様の結果を得るでしょうと思いました(整数値ですが、倍精度* 2の乗算を伴わない)。しかし、私は完全なPromise
オブジェクトを取得:
function opThatResolves() {
return Promise.resolve(1);
}
function opThatWillBeForwarded(x) {
return new Promise(function(resolve, reject) {
reject(yetAnotherNestedPromise(x));
})
}
function yetAnotherNestedPromise(x) {
return Promise.resolve(-x);
}
opThatResolves()
.then(x => opThatWillBeForwarded(x))
.then(x => x * 2)
.then(x => x * 2)
.then(x => console.log("Resolved: " + x))
.catch(x => console.log("Rejected: " + x))
なぜreject
約束がresolve
が行ったようyetAnotherNestedPromise
から返された "アンラップ" しないのですか?