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

proposal-promise-settle

v0.0.1

Published

Proposal for Promise.settle and AggregateError

Downloads

7

Readme

Proposal: Promise.settle & AggregateError

This repository is a work in progress and not seeking feedback yet.

Example

let results = await Promise.settle([
  Promise.resolve(1),
  Promise.reject(new Error('one')),
  Promise.resolve(2),
  Promise.reject(new TypeError('two')),
]);

results; // => { fulfilled: [1, 2], rejected: [Error, TypeError] }

if (results.rejected.length) {
  throw new AggregateError(results.rejected);
  /*
    AggregateError:
        Error: one
            at Object.<anonymous> (/path/to/example.js:3:18)
        TypeError: two
            at Object.<anonymous> (/path/to/example.js:5:18)
        at Object.<anonymous> (/path/to/example.js:14:9)
        at Object.<anonymous> (/path/to/example.js:14:9)
        at ...
  */
}

Prior Art

Motivation

Promise.settle motivation

await Promise.all([
  fetch({ method: 'POST', url: 'https://example.com/api/entities', body: ... }),
  fetch({ method: 'POST', url: 'fails', body: ... }),
  fetch({ method: 'POST', url: 'https://example.com/api/entities', body: ... }),
]);

When using Promise.all, a single failure means that the promise is rejected immediately and you have no way to know:

  • Which items succeeded
  • Which items failed (other than the first)

Because the promise is rejected immediately, it does not wait to find out the statuses of the unresolved promise. So you still have promises being run while you're already handling their "failure".

try {
  await Promise.all(...);
} catch {
  // promises are still running above
}

[WIP]

AggregateError motivation

Today, when you have a set of errors, there's no clear way to collect them into a single error. Developers end up encoding a subset of information inside error messages or ignore the original errors altogether.

For example, if you were writing a bunch of files and some of them failed:

let failed = [];

await Promise.all(files.map(async file => {
  try {
    await readFile(file);
  } catch {
    failed.push(file);
  }
}));

if (failed.length) {
  throw new Error(`Some files failed to write: ${failed.join(', ')}`);
}

However, there are many different reasons a file could fail to write, and there's lots of metadata on our errors that we are missing out on:

Error {
  message: "ENOENT: no such file or directory, open 'missing-file'"
  errno: -2,
  syscall: "open",
  code: "ENOENT",
  path: "missing-file"
}

But if we had an AggregateError class which held many different errors, we could throw something more meaningful.

let errors = [];

await Promise.all(files.map(async file => {
  try {
    await readFile(file);
  } catch (err) {
    errors.push(err);
  }
}));

if (errors.length) {
  throw new AggregateError(errors);
}

Using it like this:

try {
  await readFiles();
} catch (err) {
  if (err instanceof AggregateError) {
    for (let error of err) {
      /*
        Error {
          message: "ENOENT: no such file or directory, open 'missing-file'"
          errno: -2,
          syscall: "open",
          code: "ENOENT",
          path: "missing-file"
        }
      */
    }
  }
}

[WIP]

All Together

[WIP]

let {rejected} = await Promise.settle(files.map(readFile));
let errs = rejected.filter(e => e.code !== 'ENOENT');
if (errs.length) throw new AggregateError(errs);