@simsieg/sleepjs

Asynchronous sleep with better time functions

Usage no npm install needed!

<script type="module">
  import simsiegSleepjs from 'https://cdn.skypack.dev/@simsieg/sleepjs';
</script>

README

SleepJS

Build Status Coverage Status

Asyncronus sleep functions for Node.js.

Installation

Install sleepjs with npm:

npm install sleepjs

Or with yarn:

yarn add sleepjs

Usage

The default function sleeps for a time given in milliseconds. But also different sleep functions can be required.

Async / Await

The sleep timer can be awaited with async / await.

const sleep = require('sleepjs')

async function myFunction () {
  await sleep(500)
  console.info('500 ms later')
}

myFunction()

Promise

The Promise returns the value of slept milliseconds when it resolves.

const sleepMinutes = require('sleepjs').sleepMinutes

function myFunction () {
  return sleepMinutes(5)
    .then(time => {console.info(`${time} ms later`)})
}

myFunction() // Will print: '300000 ms later'

Concurrent

Different sleep instances can be run and awaited concurrently with Promise.all.

const sleep = require('sleepjs')

async function myFunction () {
  await Promise.all([sleep(100), sleep(100), sleep(200)])
}

myFunction() // Will take only slightly more than 200 ms

Functions

Sleepjs includes several functions to wrap common sleep times:

const sleep = require('sleepjs').sleep
const sleepMilliseconds = require('sleepjs').sleepMilliseconds
const sleepSeconds = require('sleepjs').sleepSeconds
const sleepMinutes = require('sleepjs').sleepMinutes
const sleepHours = require('sleepjs').sleepHours
const sleepDays = require('sleepjs').sleepDays