Awesome Open Source
Awesome Open Source

SleepJS

Build Status Coverage Status Downloads

Asynchronus sleep functions with typescript support.

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')

const myFunction = async () => {
  await sleep(500)
  console.info('It is 500 ms later now')
}

myFunction()

Promise

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

const { sleepMinutes } = require('sleepjs')

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')
const { sleepMilliseconds } = require('sleepjs') // Same as sleep
const { sleepSeconds } = require('sleepjs')
const { sleepMinutes } = require('sleepjs')
const { sleepHours } = require('sleepjs')
const { sleepDays } = require('sleepjs')

Import with typescript is also possible.

import {
  sleep,
  sleepMilliseconds,
  sleepSeconds,
  sleepMinutes,
  sleepHours,
  sleepDays,
} from 'sleepjs'

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Javascript (1,535,723
Typescript (263,621
Nodejs (55,313
Node (12,922
Js (9,737
Npm (5,830
Async (3,213
Npm Package (2,817
Yarn (1,630
Time (1,471
Promise (1,434
Async Await (614
Sleep (204
Wait (99
Pause (54
Milliseconds (49
Related Projects