Using rsvp.js or any other promises/A+ implementation how can I turn code like...
console.log('step 1');
setTimeout(function() {
console.log('step 2');
setTimeout(function() {
console.log('step 3');
}, 100);
}, 300);
into a promises implementation?
Create a delay function which returns a Promise and it actually resolves it after the specified time elapses in setTimeout
, like this
function delay(time) {
return new RSVP.Promise(function (resolve) {
setTimeout(resolve, time);
});
}
and then you can invoke it like this
console.log("step 1");
delay(3000)
.then(function () {
console.log("step 2");
return delay(1000);
})
.then(function () {
console.log("step 3");
});