Needles to say I am doing something wrong. I need some help figuring out what it is.
Consider the following code (a simplified version of my problem):
function testTimer(time, msg,resolve){
console.log(arguments.callee.name);
window.setTimeout(
function() {
console.log("MSG:", msg);
if(resolve !== undefined)
resolve(1);
}, time * 1000);
}
new Promise(function(resolve, reject) {
testTimer(1, 'ONE', resolve);
}).then(function(resolved){testTimer(9, 'TWO');}, function(rejected){alert("Rejected!", reject)})
.then(function(resolved){testTimer(1, 'THREE'); }, function(rejected){alert("Rejected!", reject)});
The expected output is:
ONE
TWO
THREE
Instead since the first then requires 9 seconds to execute and the second then requires 1 second to execute I get:
ONE
THREE
TWO
The question is simple: How can I get the then's to wait for eachother ?
Thanks!