我已经将我的代码重组为承诺,并构建了一个由多个回调组成的精彩长扁平承诺链.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) 我已经阅读了几个使用JavaScript生成器的代码示例,例如这个.我能想到的最简单的生成器使用块是这样的:
function read(path) {
return function (done) {
fs.readFile(path, "file", done);
}
}
co(function *() {
console.log( yield read("file") );
})();
Run Code Online (Sandbox Code Playgroud)
这确实打印出了内容file,但我的挂断是在哪里done调用.看起来,yield是一个语法糖,用于包装它在回调中返回的内容并适当地分配结果值(至少在co将错误参数抛给回调的情况下).我对语法的理解是否正确?
使用done时看起来像什么yield?
javascript ×2
bluebird ×1
co ×1
es6-promise ×1
generator ×1
node.js ×1
promise ×1
scope ×1
yield ×1