npm.io
0.4.0 • Published 1 year ago

waitwait

Licence
MIT
Version
0.4.0
Deps
0
Size
13 kB
Vulns
0
Weekly
0
Stars
2

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

or with pnpm:

pnpm add waitwait

or your favorite package manager...

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

Keywords