I seem to be making a mistake and I need some assistance in figuring out what it is.
Here is a simplified version of the code causing the issue:
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 should be:
ONE
TWO
THREE
However, due to the different execution times of the two 'then' functions, I get:
ONE
THREE
TWO
My question is simple: How can I make the 'then' functions wait for each other?
Thank you for your help!