jma*_*zin 1 javascript node.js promise q
我有类似下面的内容,并且想知道是否有"连锁"方式来做,或者如果我没有标记,这代表了一种气味.谢谢!
var promises = Q.all(returns_a_promise()).then(returns_array_of_promises);
var more_promises = Q.all(promises).then(returns_another_array_of_promises);
var even_more_promises = Q.all(more_promises).then(yet_another_array_o_promises);
Q.all(even_more_promises).then(function () {
logger.info("yea we done");
});
Run Code Online (Sandbox Code Playgroud)
理想情况如下:
Q.all(returns_a_promise())
.then(returns_array_of_promises)
.all(returns_another_array_of_promises)
.all(yet_another_array_o_promises)
.all(function () {
logger.info("yea we done");
});
Run Code Online (Sandbox Code Playgroud)
只需Q.all直接从函数返回,就像这样
Q.all(returns_a_promise())
.then(function() {
return Q.all(array_of_promises);
})
.then(function() {
return Q.all(array_of_promises);
})
.then(function() {
return Q.all(array_of_promises);
})
.done(function() {
logger.info("yea we done");
});
Run Code Online (Sandbox Code Playgroud)
例如,
Q.all([Q(1), Q(2)])
.spread(function(value1, value2) {
return Q.all([Q(value1 * 10), Q(value2 * 10)]);
})
.spread(function(value1, value2) {
return Q.all([Q(value1 * 100), Q(value2 * 100)]);
})
.spread(function(value1, value2) {
return Q.all([Q(value1 * 1000), Q(value2 * 1000)]);
})
.done(function() {
console.log(arguments[0]);
})
Run Code Online (Sandbox Code Playgroud)
会打印
[ 1000000, 2000000 ]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1632 次 |
| 最近记录: |