I want to do something really simple, but i don't understand a little thing ...
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();
I would like each element of my funcs array to be "treated" sequentially, the output would then be something like :
treated first
//2 seconds later
treated second
//2 seconds later
treated third
//2 seconds later
treated fourth
I cannot achieve that :( it should be simple , i don't catch something :(
See Question&Answers more detail:os