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 🙏

© 2026 – Pkg Stats / Ryan Hefner

promise-race-all

v0.1.0

Published

Async iterator that yields promise results in completion order

Readme

promise-iterator

Async iterator that yields promise results in completion order.

Works in Node.js, browsers, Deno — anywhere that supports async iterators. Zero dependencies.

Why?

| Method | Fails fast? | Streams results? | Captures errors? | |--------|-------------|------------------|------------------| | Promise.all | Yes | No | No | | Promise.allSettled | No | No | Yes | | PromiseIterator | No | Yes | Yes |

PromiseIterator gives you results as they complete, without waiting for all promises to finish, and without failing on the first rejection.

Installation

npm install promise-race-all

Usage

Basic iteration

import { PromiseIterator } from 'promise-race-all';

const promises = [
    fetch('/api/slow').then(r => r.json()),
    fetch('/api/fast').then(r => r.json()),
    fetch('/api/medium').then(r => r.json()),
];

// Results arrive in completion order (fast first, then medium, then slow)
for await (const entry of new PromiseIterator(promises)) {
    if ('result' in entry) {
        console.log(`Request ${entry.index} completed:`, entry.result);
    } else {
        const message = entry.error instanceof Error ? entry.error.message : String(entry.error);
        console.log(`Request ${entry.index} failed:`, message);
    }
}

Collect all results

const results = await new PromiseIterator(promises).all();

// Results are in completion order, not input order.
// Use 'in' to narrow the discriminated union:
const successes = results.filter(r => 'result' in r);
const failures = results.filter(r => 'error' in r);

// To restore original order:
const sorted = [...results].sort((a, b) => a.index - b.index);

Early exit

for await (const { result } of new PromiseIterator(promises)) {
    if (result?.isWhatWeNeed) {
        break;
    }
}
// Note: remaining promises continue running in the background.
// PromiseIterator does not cancel them.

Tracking progress

const iterator = new PromiseIterator(promises);
console.log(`Starting with ${iterator.size} promises`);

for await (const { index } of iterator) {
    console.log(`Completed ${index}, ${iterator.size} remaining`);
}

API

new PromiseIterator<T>(promises: Promise<T>[])

Creates an async iterator over the given promises. An empty array is valid — the iterator completes immediately and all() returns [].

PromiseResult<T>

type PromiseResult<T> =
    | { result: T; index: number }    // Resolved
    | { error: unknown; index: number }; // Rejected

Use 'result' in entry or 'error' in entry to narrow the union. Rejection values are preserved as-is.

Properties

  • size — Number of promises still pending. Decrements as promises settle.

Methods

  • next() — Returns the next completed result
  • all() — Collects all results into an array (in completion order)

Async iteration

for await (const result of new PromiseIterator(promises)) {
    // ...
}

License

LGPL-3.0