/ Published in: JavaScript
Warp settimeout function in promise
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var milllisecond = 4000; function wait(ms){ console.log('started'); // new promise return new Promise(function(resolve,reject){ console.log(this); window.setTimeout(function(){ // resolve inside the callback of settimeout resolve(ms); reject(ms); },ms); }); }; wait(milllisecond).then(finish); function finish(a){ console.log(1+a) };