From 4a661d70f2eec0a2dec891d8fed38bdc5cfeaf55 Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Thu, 14 Dec 2017 00:10:56 +0330 Subject: [PATCH 1/2] add sleep function - making delays in async functions --- snippets/sleep.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 snippets/sleep.md diff --git a/snippets/sleep.md b/snippets/sleep.md new file mode 100644 index 000000000..4a7e8916b --- /dev/null +++ b/snippets/sleep.md @@ -0,0 +1,12 @@ +### Sleep + +If you have an async function and you want to delay executing part of it. you can put your async function to sleep(in miliseconds). + +```js +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); +// async function sleepyWork() { +// console.log('I\'m going to sleep for 1 second.'); +// await sleep(1000); +// console.log('I woke up after 1 second.'); +// } +``` From 9980dbac0f596e99e08853057e88a4d49f0caa1d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 10:34:29 +0200 Subject: [PATCH 2/2] Update sleep.md --- snippets/sleep.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/sleep.md b/snippets/sleep.md index 4a7e8916b..c64ed3917 100644 --- a/snippets/sleep.md +++ b/snippets/sleep.md @@ -1,6 +1,6 @@ ### Sleep -If you have an async function and you want to delay executing part of it. you can put your async function to sleep(in miliseconds). +Delay executing part of an `async` function, by putting it to sleep, returning a `Promise`. ```js const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));