Create timeout in an async function

To throttle the execution of our async function we will create a timeout method.

SNP 015 #B96 Filed webdev js

Sometimes you just want to delay the execution in your async functions. This could be useful if you create any recursive or waterfall logic.

To do this just create a timeout function:

javascript
timeout(ms) {
  return new Promise((resolve) => window.setTimeout(resolve, ms))
}

Then use it in your async functions like this:

javascript
await this.timeout(2000)