JSPM

sleep-ts

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2379
  • Score
    100M100P100Q112616F
  • License MIT

Sleep / pause execution using Promises and flexible time formats

Package Exports

  • sleep-ts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sleep-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

sleep-ts npm npm Build Status codecov Known Vulnerabilities

Pause JavaScript execution for a specified amount of time using Promises. Works elegantly with async/await and accepts flexible time formats like '3s' and '1h' (any string that can be parsed by the ms package). Implemented in TypeScript.

Installation

$ yarn add sleep-ts
# or
$ npm install --save sleep-ts

Usage

import {sleep} from 'sleep-ts';
// or
const sleep = require('sleep-ts').sleep;

// With async/await
(async function slowHelloWorld() {
    console.log('Hello');

    // a number argument is treated as milliseconds, so this will
    // sleep for 1 second
    await sleep(1000);

    // a string argument is parsed, so this will sleep for 2 seconds
    await sleep('2s');

    console.log('World');
})()

// With raw Promises
sleep(4000).then(() => {
    console.log('PING');

    // If you pass a second argument, the sleep Promise will
    // resolve with the given value.
    return sleep(3000, 'PONG');
}).then(response => {
    console.log(response);
});

Output:

Hello
# 3 seconds later
World
# 1 second later
PING
# 3 seconds later
PONG

Alternatives

  • If you don't care about the fancy time format parsing, you can use sleep-promise, which might save you a subdependency on the ms package.
  • Use setTimeout if you're okay with using callbacks.

Other Languages

License

MIT