Asynchronous sleep with better time functions and typescript bindings


Keywords
async, await, break, pause, promise, sleep, time, typescript, async-await, javascript, js, milliseconds, node, node-js, nodejs, npm, npm-package, sleep-functions, wait, yarn
License
MIT
Install
npm install sleepjs@1.0.0

Documentation

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