javascript - How to convert nested setTimeouts to promises -
using rsvp.js or other promises/a+ implementation how can turn code like...
console.log('step 1'); settimeout(function() { console.log('step 2'); settimeout(function() { console.log('step 3'); }, 100); }, 300);
into promises implementation?
create delay function returns promise , resolves after specified time elapses in settimeout
, this
function delay(time) { return new rsvp.promise(function (resolve) { settimeout(resolve, time); }); }
and can invoke this
console.log("step 1"); delay(3000) .then(function () { console.log("step 2"); return delay(1000); }) .then(function () { console.log("step 3"); });
Comments
Post a Comment