2
我想要做的事很簡單,但我不明白一件小事......的NodeJS/Q:鏈接承諾依次
var Q = require('q');
var funcs = ["first", "second", "third", "fourth"];
function main(){
// really don't know how to chain sequentially here ...
var result = Q();
funcs.forEach(function (f) {
result = treat(f).then(f);
});
}
function treat(t){
var deferred = Q.defer();
setTimeout(function(){
deferred.resolve("treated "+ t);
},2000);
return deferred.promise;
}
main();
我想我的funcs中數組的每一個元素被「處理「順序,輸出會再是這樣的:
treated first
//2 seconds later
treated second
//2 seconds later
treated third
//2 seconds later
treated fourth
我不能做到這一點:(它應該是簡單的,我不明白的東西:(