<分区>
我需要链接一些异步操作。我正在尝试编写使用 express 的 Parse.com 云代码。 (我认为 express 是 promise 支持的来源)。我明白为什么 promise 很有值(value),但我仍然不确定有几件事。首先是如何收集顺序操作的结果。下面的代码说明:
function doAsnychThingsInSequence(params) {
return doThing0(params).then(function(resultOfThing0) {
// thing1 depends on resultOfThing0
doThing1(resultOfThing0);
}).then(function(resultOfThing1) {
// here is where I am confused.
// thing2 depends on the results of thing0 and thing1
doThing2(resultOfThing0 /* out of scope?? */, resultOfThing1);
}, function(error) {
// handle error
});
}
thing1 完成后,我需要两个操作的结果。我想我可以在函数顶部分配一个变量并将其分配给第一个回调中的第一个结果,这是正确的方法吗?我想,我困惑的核心是问题二......
return doThing0(params0).then(function(resultOfThing0) {
doThing1(resultOfThing0);
// what does return mean here? does what I return here relate to the
// parameters of the next function?
return "foo";
}).then(function(param) {
// what is in param? is it "foo"?
}, function(error) {
});