I'm attempting to run a series of functions based upon the Q
API using their first strategy for sequences. This suggests the pattern:
var funcs = [foo, bar, baz, qux];
var result = Q(initialVal);
funcs.forEach(function (f) {
result = result.then(f);
});
return result;
What structure are each of the functions within the array meant to take? I am quite confused about when to use the return def.promise;
. Is that simply always the last line? Will it frequently or always immediately follow def.resolve(someVar)
. Is something like this then ended structure?
function foo(f){
var def = Q.defer();
f++;
def.resolve(f);
return def.promise;
}
So that each subsequent function within the array will receive the newly calculated value of f
: in this case, if var initialVal = 1;
and four functions each incrementing f++
, the returned result will be 4
? How do I access that returned value? console.log(result)
prints { state: 'pending' }
.