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

another-promise-array

v1.0.0

Published

A modified Array to help handle Promise.all() chains

Downloads

3

Readme

PromiseArray

A helper class for Promise.all() calls.

What can it do?

Promise.all() takes an array of Promises, runs them in a chain, and resolves an array of returned values if they were all successful. If any of them fails, it stops executing, and rejects with a single error.

Promise.all([
  promise1,
  promise2,
  promise3
])
.then(result => console.log(result))      // displays array of results
.catch(err => console.log(err));          // displays single error

The problem is that Promise.all() can't tell you exactly which Promise has failed.

Of course, you could use Promise.allSettled(), which would tell which one has failed. However, it would also always run the entire Promise chain, regardless of errors. This may be undesired if you want it to stop at the first error.

This is what PromiseArray solves.

How to use?

PromiseArray is a descendant of the standard Javascript array. Here is how to use it.

import { PromiseArray } from 'another-promise-array';

Promise.all(
  new PromiseArray([
    {
      promise: <FIRST PROMISE>,
      errorMessage: 'Promise 1 failed!'
    },
    {
      promise: <SECOND PROMISE>,
      errorMessage: 'Promise 2 failed!'
    },
    {
      promise: <THIRD PROMISE>,
      errorMessage: 'Promise 3 failed!'
    }

    ...

  ])
)
    .then(res => console.log(res))
    .catch(err => console.log(err.message));

If any of the Promises fail, it will throw an object containing both the specified errorMessage and the actual error thrown by the Promise.

For example, if the Promise was an Axios call, this is what it may look like:

{
    //  Your error message
    message: 'Promise 2 failed!',

    //  The actual error message from the Promise
    error: {
        message: 'Request failed with status code 404',
        name: 'Error',
        stack: 'Error: Request failed with status code 404',
        config: {
            url: '/api_endpoint',
            method: 'get',
            headers: {
            Accept: 'application/json, text/plain, */*'
            },
            ...
        }
    }
}

Note that the error message can be any type, not just a string.