Files
30-seconds-of-code/snippets/sleep.md
Isabelle Viktoria Maciohsek 5cb69e3c5c Update snippet descriptions
2020-10-22 20:24:30 +03:00

21 lines
448 B
Markdown

---
title: sleep
tags: function,promise,intermediate
---
Delays the execution of an asynchronous function.
- Delay executing part of an `async` function, by putting it to sleep, returning a `new Promise()`.
```js
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
```
```js
async function sleepyWork() {
console.log("I'm going to sleep for 1 second.");
await sleep(1000);
console.log('I woke up after 1 second.');
}
```