我已经将我的代码重组为承诺,并构建了一个由多个回调组成的精彩长扁平承诺链.then().最后我想返回一些复合值,并且需要访问多个中间承诺结果.但是,序列中间的分辨率值不在最后一个回调的范围内,我该如何访问它们?
function getExample() {
return promiseA(…).then(function(resultA) {
// Some processing
return promiseB(…);
}).then(function(resultB) {
// More processing
return // How do I gain access to resultA here?
});
}
Run Code Online (Sandbox Code Playgroud) 我在这里遵循规范,我不确定它是否允许使用多个参数调用onFulfilled.例如:
promise = new Promise(function(onFulfilled, onRejected){
onFulfilled('arg1', 'arg2');
})
Run Code Online (Sandbox Code Playgroud)
这样我的代码:
promise.then(function(arg1, arg2){
// ....
});
Run Code Online (Sandbox Code Playgroud)
会收到arg1和arg2?
我不关心任何具体的承诺如何实现它,我希望密切关注w3c规范的承诺.
我无法通过所有论点.我的承诺回调只收到一个而不是三个:
var asyncFunction= function(resolve) {
setTimeout(function() {
resolve("Some string that is passed", "and another", "third");
}, 1000);
};
var promiseFunction = function () {
var deferred = Q.defer();
asyncFunction(deferred.resolve);
return deferred.promise;
};
promiseFunction().then(function() {
// Only one argument is passed here instead of 3
// { '0': 'Some string that is passed' }
console.log(arguments);
});
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?