How could I fire off promises one after the other?
waitFor(t)
, is a function that returns a promise that resolves after t
time. What I wish to be able to do with that is:
waitFor(1000) Then when finished, console.log('Finished wait of 1000 millis') then
waitFor(2000) Then when finished, console.log('Finished wait of 2000 millis') then
waitFor(3000) Then when finished, console.log('Finished wait of 3000 millis')
Here is what I tried:
waitFor(1000).then(function(resolve, reject) {
console.log(resolve);
}).then(waitFor(2000).then(function(resolve, reject) {
console.log(resolve);
})).then(waitFor(3000).then(function(resolve, reject) {
console.log(resolve);
}));
Unfortunately this console.logs the statements each 1 second after another, which means that the promises where all called at once.
I managed to fix this with callbacks like so, yet that makes everything very ugly:
waitFor(1000).then(function(resolve, reject) {
console.log(resolve+' @ '+(new Date().getSeconds()));
waitFor(2000).then(function(resolve, reject) {
console.log(resolve+' @ '+(new Date().getSeconds()));
waitFor(3000).then(function(resolve, reject) {
console.log(resolve+' @ '+(new Date().getSeconds()));
});
});
});
So how should I do this with promises that makes it work, yet isn't using ugly callback hell?
Undesired result: http://jsfiddle.net/nxjd563r/1/
Desired result: http://jsfiddle.net/4xxps2cg/