SkillAgentSearch skills...

Waitgroup.js

A way of signalling completion of multiple concurrent tasks, similar to the sync.WaitGroup in Go Lang.

Install / Use

/learn @dtjm/Waitgroup.js
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

waitgroup.js - concurrency synchronization primitive

This is a translation of Go's sync.WaitGroup for JavaScript, used extensively in TextDrop.

Use this if you want run several asynchronous tasks concurrently and run a function when all the results come back. (E.g. if you want to make multiple web requests or database queries, then use them together).

This is much better than making each request in serial, and takes advantage of JavaScript's asynchronous design to boost the response time of your program by making your requests concurrent.

Usage

var wg = new WaitGroup;

wg.add();
initResource1().on("done", function(){
  wg.done();
});

wg.add();
initResource2().on("done", function(){
  wg.done();
});

wg.add();
initResource3().on("done", function(){
  wg.done();
});

wg.wait(function(){
  console.log("All done!")
});

Methods

add()

Increments the WaitGroup counter

done()

Decrements the WaitGroup counter. When the counter becomes 0, the function passed to wait() will be called.

wait(func)

Registers a callback to be called when all tasks in the WaitGroup are done.

Tests

Run the tests with npm test

Related Skills

View on GitHub
GitHub Stars78
CategoryDevelopment
Updated2y ago
Forks8

Languages

JavaScript

Security Score

65/100

Audited on Sep 7, 2023

No findings