Wondering what’s next for npm?Check out our public roadmap! »

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

1.0.1 • Public • Published

promisolock

Build Status Coverage

Useful to handle the number of promises to start in a Promise.all for long and resource-intensive operations.

Install

$ npm install promisolock
# or
$ yarn add promisolock

Usage

const promisolock = require("promisolock");
 
 
promisolock(2).all([
 () => Promise.resolve("P1"), // Starts immediately
 () => Promise.resolve("P2"), // Starts immediately
 () => Promise.resolve("P3"), // Await for one of the above promises to be fulfilled
 () => Promise.resolve("P4"), // etc
]).then(console.log).catch(console.error);
//=> ["P1", "P2", "P3", "P4"]
 
(function () {
 
 const array = ["P1", "P2", "P3", "P4"];
 await promisolock().all(
  array.map((str) => () => Promise.resolve(str))
 )
 //=> ["P1", "P2", "P3", "P4"]
 
}());

API

promisolock(max).all(callbacks)

max?: number; 
// Number of promises to start simultaneously, default 1.
 
callbacks?: Array<() => any> 
// Promises to start in a promise.all, default [].

Inspired by @SlimIO/Lock

Install

npm i promisolock

DownloadsWeekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

4.34 kB

Total Files

5

Last publish

Collaborators

  • avatar