return makeFirstPromise()
.then(function(res1) {
(...)
})
.then(function(res2) {
(...)
})
.then(function(res3) {
// **here I need to access res1**
});
我想知道當我需要訪問前一個承諾時,是否有最佳做法導致我的承諾鏈的後續功能。關於JavaScript Promise的最佳做法
我看到了兩個可能的解決方案:
var r1;
return makeFirstPromise()
.then(function(res1) {
r1 = res1;
(...)
})
.then(function(res2) {
(...)
})
.then(function(res3) {
console.log(r1);
});
或窩後的第一個承諾,但它在視覺上打破了鏈序列:
return makeFirstPromise()
.then(function(res1) {
(...)
return secondPromise(res2)
.then(function(res3) {
console.log(res1);
});
});
任何想法?
的[?如何訪問的。那麼()鏈先前承諾的結果(精確複製http://stackoverflow.com/questions/28250680/how-do-i -access-previous-promise-results-in-a-then-chain) – Bergi 2015-01-31 10:58:08