I'm using node.js with Q as promise implementation.
For some reason I have to build a few promise with a loop. In the "real" code, of course, I do not use constant in "for" loop.
I have an issue when I give i
as parameter to my function buildPromiseForIdx
. I expect to pass the value of i
and expect the following result in the console.
-3
*3
but the code displays:
-3
*2
Here is the code:
function loop(promise, fn) {
return promise.then(fn).then(function (result) {
return !result ? loop(Q(result), fn) : result;
});
}
function buildPromiseForIdx(i) {
return getIdx(i*10).then(parseIdx);
}
// building promises for all idx page
var promises= [];
for (var i = 3 ; i >= 3 ; i--) {
log.debug('-'+i);
promises.push(loop(Q(false), function () { log.debug('*'+i);return buildPromiseForIdx(i)}));
}