DEV Community

Cover image for Await a setTimeout
Francis Provost
Francis Provost

Posted on


Await a setTimeout

Even thought setTimeout isn't always the best solution we always end up using it for some reason. Since I quite like the async/await syntax I create a small snippets to await a setTimeout.

// === Snippet ===
const wait = (timeToDelay) => new Promise((resolve) => setTimeout(resolve, timeToDelay));

// === Usage ===
// Some code before
await wait(1000); // wait 1s
// Code runs after 1s
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

arqex profile image
Javier Marquez

Nice snippet! Thanks for sharing!

damarowen profile image
Damar Owen

glad i'm found this, this 2 line code really helps my problem

thanks brad

Visualizing Promises and Async/Await 🤓

async await

☝️ Check out this all-time classic DEV post on visualizing Promises and Async/Await 🤓