Need private packages and team management tools?Check out npm Teams »

sleepjs

3.0.1 • Public • Published

SleepJS

Build Status Coverage Status Downloads

Asynchronus 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

Install

npm i sleepjs

DownloadsWeekly Downloads

3,060

Version

3.0.1

License

MIT

Unpacked Size

48.8 kB

Total Files

17

Last publish

Collaborators

  • avatar