waitwait
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

WaitWait

WIP

Tiny implementation of Golang's WaitGroup and Unix's sleep for Javascript (browser and Node.js), with promises and zero dependencies.

Install

With NPM:

npm install waitwait --save

or with Yarn:

yarn add waitwait

Usage

Sleep

sleep (wait) 1 seconde:

import {sleep} from 'waitwait';

console.log('Start');

await sleep(1000);

console.log('End');

WaitGroup

Waits until the routine is done:

import {WaitGroup} from 'waitwait';

const wg = new WaitGroup();

wg.add();

console.log('Start');

setTimeout(() => {
  wg.done();
}, 1000);

await wg.wait();

console.log('End');

Waits until all routines are done:

import {WaitGroup} from 'waitwait';

const wg = new WaitGroup();

wg.add(2);

console.log('Start');

setTimeout(() => {
  wg.done();
}, 1000);

setTimeout(() => {
  wg.done();
}, 4000);

await wg.wait();

console.log('End');

Waits forever:

import {WaitGroup} from 'waitwait';

const wg = new WaitGroup();

console.log('Start');

wg.add();

console.log('This process is running forever');

await wg.wait();

console.log('End');

Cancel a WaitGroup:

import {WaitGroup} from 'waitwait';

const wg = new WaitGroup();

// Add 10 routines
wg.add(10);

console.log('Start');

// Cancel all routines after 2 secondes
setTimeout(() => {
  wg.cancel();
}, 2000);

await wg.wait();

console.log('End');

LICENSE

MIT (c) 2021, Nicolas Tallefourtane.

Author

Nicolas Tallefourtane - Nicolab.net
Nicolas Talle
Make a donation via Paypal

Dependencies (0)

    Dev Dependencies (3)

    Package Sidebar

    Install

    npm i waitwait

    Weekly Downloads

    1

    Version

    0.3.0

    License

    MIT

    Unpacked Size

    8.66 kB

    Total Files

    9

    Last publish

    Collaborators

    • nicolab