Combination of async function + await + setTimeout

Your sleep function does not work because setTimeout does not (yet?) return a promise that could be awaited. You will need to promisify it manually: function timeout(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function sleep(fn, …args) { await timeout(3000); return fn(…args); } Btw, to slow down your loop you probably don’t want … Read more

Pass correct “this” context to setTimeout callback?

EDIT: In summary, back in 2010 when this question was asked the most common way to solve this problem was to save a reference to the context where the setTimeout function call is made, because setTimeout executes the function with this pointing to the global object: var that = this; if (this.options.destroyOnHide) { setTimeout(function(){ that.tip.destroy() … Read more

How can I pass a parameter to a setTimeout() callback?

In modern browsers (ie IE11 and beyond), the “setTimeout” receives a third parameter that is sent as parameter to the internal function at the end of the timer. Example: var hello = “Hello World”; setTimeout(alert, 1000, hello); More details: https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setTimeout http://arguments.callee.info/2008/11/10/passing-arguments-to-settimeout-and-setinterval/