I usually write it as
const sleep = ms => new Promise(res => setTimeout(res, ms))
since there's no reason to introduce an additional closure. You can also do that with value (that promise will resolve to) since setTimeout supports passing arguments.
setTimeout
const sleep = (ms, v) => new Promise(res => setTimeout(res, ms, v)) sleep(500, "hello").then(console.log)
Although great example const sleep = ms => new Promise(res => setTimeout(res, ms))
.then(console.log) I usually avoid to chaining in this case. Await would be great fit here.
.then(console.log)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I usually write it as
since there's no reason to introduce an additional closure.
You can also do that with value (that promise will resolve to) since
setTimeout
supports passing arguments.Although great example
const sleep = ms => new Promise(res => setTimeout(res, ms))
.then(console.log)
I usually avoid to chaining in this case. Await would be great fit here.