sleep() in JavaScript with Sleep.js

A safe way to make your JavaScript sleep. Be cautious when using it during iteration. Works fine when using traditional for loops, but won't work inside a .map or .reduce for example.

module.exports = milliseconds => {
  return new Promise(resolve => setTimeout(resolve, milliseconds));

The function returns a promise that is resolved when the timeout is reached.

Use it like this:

const sleep = require('./sleep')

const f = async () => {
  await sleep(5000)