Use setTimeout with promises or async/await

(Javascript tips) One of my favorite tiny helpers is this "wait" function that allows you to use setTimeout with promises or async/await for cleaner, more readable code.

// Define the function
const wait = (timeout) => (new Promise(resolve => setTimeout(resolve, timeout)))

/* Wait 500ms then do something */

// Using Promises

wait(500).then(() => doSomething())

// Using Async/await

await wait(500)

doSomething()

Source: @JakeDohm