I'm new into node.js and promise (Q), so please be kind. I want to chain nested promises with his executing parent chain and i can't find how to.
I've made a toy script to illustrate my pb, you can launch it with node.js :
var Q = require("q");
function init() {
return {nbIn: 0, nbOut: 0, stop: false};
}
function fn1(ctx) {
var deferred = Q.defer();
console.log("fn1:" + JSON.stringify(ctx));
setTimeout(function() {
console.log("fn1: resolve");
deferred.resolve(ctx);
}, 1000)
return deferred.promise;
}
function sub1(ctx) {
var deferred = Q.defer();
console.log("sub1:" + JSON.stringify(ctx));
setTimeout(function() {
++ctx.nbIn;
console.log("sub1: resolve");
deferred.resolve(ctx);
}, 1000);
return deferred.promise;
}
function sub2(ctx) {
var deferred = Q.defer();
console.log("sub2:" + JSON.stringify(ctx));
setTimeout(function() {
++ctx.nbOut;
if(ctx.nbOut === 3) {
console.log("sub2: resolve");
ctx.stop = true;
deferred.resolve(ctx);
}
else {
console.log("sub2: promise");
return sub1(ctx).then(sub2);
}
}, 1000);
return deferred.promise;
}
function fn2(ctx) {
console.log("fn2:" + JSON.stringify(ctx));
return sub1(ctx).then(sub2);
}
function fn3(ctx) {
console.log("fn3:" + JSON.stringify(ctx));
}
Q.fcall(init).then(fn1).then(fn2).then(fn3);
It display:
fn1:{"nbIn":0,"nbOut":0,"stop":false}
fn1: resolve
fn2:{"nbIn":0,"nbOut":0,"stop":false}
sub1:{"nbIn":0,"nbOut":0,"stop":false}
sub1: resolve
sub2:{"nbIn":1,"nbOut":0,"stop":false}
sub2: promise
sub1:{"nbIn":1,"nbOut":1,"stop":false}
sub1: resolve
sub2:{"nbIn":2,"nbOut":1,"stop":false}
sub2: promise
sub1:{"nbIn":2,"nbOut":2,"stop":false}
sub1: resolve
sub2:{"nbIn":3,"nbOut":2,"stop":false}
sub2: resolve
I would like to chain the last line sub2
with fn3
.
Any help appreciated, Thanks.