npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

promise-bin

v0.1.0

Published

Need a way to keep track of and handle a lot of similar promises without iterating over an array to find out which have resolved?

Downloads

7

Readme

The Promise Bin

Need a way to keep track of and handle a lot of similar promises without iterating over an array to find out which have resolved?

I wrote this as a way to process the results of running fstat on many files. I wanted to be able to handle the results as they came in without keeping a list of promises to check. I also wanted to know when and how my promises were resolving so that I could update a status line in the console.

What is it?

Rather than creating an array or chain of promises, or using Promise.all() or Promise.race(), wouldn't it be cool if promises could just keep track of themselves and let you know when they have something for you?

PromiseBin is basically a bucket. Chuck your promises in there and leave them be. Register a success handler and an error handler and all of the results will be piped there, as they come in.

Want to trigger an event or events when the next promise fulfills? Done.

Want to know how many promises are still pending? We've got your stats.

Installing

npm i promise-bin

Testing

npm t Will run full unit-tests + coverage. Coverage report in: ./coverage/ as HTML.

You can also use npm run test:nocoverage for just the tests.

Usage

const PromiseBin = require('promise-bin')

// Instantiate PromiseBin with fulfill and reject handlers
const bin = new PromiseBin(onFulfill, onReject)

// Add some promises
bin.add(aPromise)
bin.add(anotherPromise)
bin.add(morePromises)

// Get the return value of the next promise to be fulfilled
const nextToFulfill = await bin.nextFulfillment()

// Get the error of the next promise to be rejected
const nextToReject = await bin.nextRejection()

// This will just wait for the next promise to
// either fulfill or reject before proceeding
await bin.nextChange()

// This will wait until all of the promises have resolved
// before proceeding. (Or proceed if there are zero pending.)
await bin.noMorePending()

// Also you can get stats at any time
bin.fulfilled // => the number of promises already fulfilled
bin.rejected // => the number of promises which have rejected
bin.pending // => how many promises have yet to resolve
bin.total  // => total number processed by this instance

// And this returns an object containing all of the stats
bin.status // => { fulfilled, rejected, pending, total }

How does it work?

The onFulfill and onReject handlers passed to the constructor are directly linked to the .then() and .catch() respectively of any promise which is .add()'ed to the bin. When calling, for example .nextToFulfill(), we break the chain and replace the onFulfill with a new promise, which we return. The new promise, prior to being returned, is .then()'ed to replace the original handler and to call it with the value fulfilled. Any number of handlers can be inserted in this way, and then removed again upon the event being triggered.

Essentially, this is a multiply-linked list of promises which cascade upon resolution. Just think of a digital-quantum-waveform collapsing into a known value.

Honestly I don't know how to explain it better than that... just look at the code.

Note

I've discovered a potential flaw... I'll leave it up to you to figure out what to do with pending notification promises. For example: you have a promise which will fulfill on the next rejection, yet the promiseBin reaches zero without a single rejection... leaving your handler hanging.

Possibilities:

  • Fake a rejection using null as the error... but what about the reverse, if there is not a single fulfillment? (Because null, while an unlikely value to hrow as an error, is a perfectly valid return value.)
  • Wire the notification promises to return an object including a status as well as the return or error value so as to indicate whether it was triggered as a notification of the event requested, or to clear out the promise bin.
  • Just leave them be... If you use the bin again they'll still be there and if you chuck it, they should get garbage-collected and simply not run whatever "then" code you've assigned, or whatever code comes after your "await".

Author

Jeremy Rumble https://medium.com/@jmrumble