Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-make-counter

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-make-counter

A simple function that makes a counter-based promise, which can be incremented and decremented, and it resolves once its counter reaches zero.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
25K
decreased by-19.92%
Maintainers
0
Weekly downloads
 
Created
Source

Promise Make Counter

A simple function that makes a counter-based promise, which can be incremented and decremented, and it resolves once its counter reaches zero.

This is an alternative to creating a promise that waits on many other promises to resolve. You can do that, but creating many promises can be surprisingly slow, instead you could use this package, switch internally to using callbacks-style APIs rather than async/await-style APIs, and just increment the counter at the start of each callback, and decrement it at its end, once its job is done.

Install

npm install --save promise-make-counter

Usage

import fs from 'node:fs';
import makeCounterPromise from 'promise-make-counter';

// Let's create a counter-based promise

const {promise, isPending, increment, decrement} = makeCounterPromise ();

// Now let's do some work that's tracked by our counter-based promise
// It's important to always increment at the start, and decrement at the end

for ( const filePath of filePaths ) {

  increment ();

  fs.readFile ( filePath, 'utf8', ( error, data ) => {

    // Do something here...

    decrement ();

  });

}

// Let's wait for the counter-based promise to resolve
// This will happen once its internal counter will reach zero

await promise;

License

MIT © Fabio Spampinato

Keywords

FAQs

Package last updated on 30 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc