Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including sleepjs with all npm packages installed. Try it out:

var sleepjs = require("sleepjs")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

sleepjs v3.0.1

Asynchronous sleep with better time functions

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
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free