I am trying to build an array of Promises chains and get them to execute in order. Each chain completes before the next entry in the array executes. For example: [connect1, connect2, connect3] where each "task" contain a chain of some number of steps.
One of the big problems I have is being able to build the chain and add it to the array--since it has already began execution.
Some of the test code I have been toying with is here:
function step1() {
return new Promise(resolve => {
// Normally something asnyc here
console.log("step1:", this);
resolve();
});
}
function step2() {
return new Promise(resolve => {
// Normally something asnyc here
console.log("step2:", this);
resolve();
});
}
function step3() {
return new Promise(resolve => {
// Normally something asnyc here
console.log("step3:", this);
resolve();
});
}
function promiseSeq( tasks, state ) {
let current = Promise.resolve();
for (let k = 0; k < tasks.length; ++k) {
var taskWithState = tasks[k];
if (typeof state !== 'undefined') {
taskWithState = taskWithState.bind(state);
}
current = current.then(taskWithState);
}
return current;
}
function buildChain(idx) {
// Build the connection chain (with state)
//------------------------------
var s = { conn: idx }; // some state
var q = [ step1, step2, step3 ];
return promiseSeq(q, s);
}
function streamConnect() {
// Build array of chains
//------------------------------
var q = [ ];
q.push(buildChain(1)); // e.g. connect1
q.push(buildChain(2)); // e.g. connect2
q.push(buildChain(3)); // e.g. connect3
var p = Promise.each(q, function(f) {return ( f )});
// Process promises...
p.then(function() {console.log("done")})
.catch(function(err) {console.error("catch:",err)})
return;
}
Once I have the chains for each "task" in the array, I want to execute the chain in order. So my goal is to get the output to be:
step1: Object {conn: 1}
step2: Object {conn: 1}
step3: Object {conn: 1}
step1: Object {conn: 2}
step2: Object {conn: 2}
step3: Object {conn: 2}
step1: Object {conn: 3}
step2: Object {conn: 3}
step3: Object {conn: 3}
Instead using my code I see:
step1: Object {conn: 1}
step1: Object {conn: 2}
step1: Object {conn: 3}
step2: Object {conn: 1}
step2: Object {conn: 2}
step2: Object {conn: 3}
step3: Object {conn: 1}
step3: Object {conn: 2}
step3: Object {conn: 3}
I am very green when it comes to Promises and I am trying to understand ( in no specific order):
1. Why the promise execution appears to interleave (all step1's done, then step2, etc)?
2. How can I get serialized execution line my expected output above?
3. Can the chains be setup in a deferred manner? I saw my line current = current.then(taskWithState); ultimately calls async.invoke with Bluebird, but I didn't see a way to avoid this.
Any advice or help would be much appreciated.