In JavaScript, it is fairly standard to use things like setTimout(fn, 500) to do a delay and then run a function. This does not use the Promise pattern which is becoming the dominant pattern for handling asynchronous programming (at least until await, async replace it a few years down the road). So I will write some quick code to do a simple wait.
function wait(duration) {
return new Promise((resolve, reject) => {
setTimeout(resolve, duration)
})
}
This makes some nice looking code like:
wait(5000).then(doStuff);
This is also the basic example on MSDN for Promises.
This is nice because it separates control flow from parameters.
No comments:
Post a Comment