DEV Community

Cover image for Await a setTimeout

Await a setTimeout

francisprovost profile image Francis Provost ・1 min read

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

Discussion (1)

Editor guide
arqex profile image
Javier Marquez

Nice snippet! Thanks for sharing!