Giter Site home page Giter Site logo

when-all-settled's Introduction

when-all-settled

Tiny javascript module that functions like javascript's Promise.all, but it does not mind if any of the promises reject.

Creates a Promise that resolves when all promises passed to it have settled (= either resolved or rejected). This is almost the same as Promise.all, except it does not reject if some of the given promises reject. Rejected promises simply result in a value undefined in the array of results.

DEPRECATED: Nearly the same function has been standardised in ES2020 as Promise.allSettled(…) Best to use that instead, if needed through e.g. Babel or adding a shim.

Install

npm install when-all-settled

…or equivalent.

Use

Example case, fetching some resources:

import whenAllSettled from 'when-all-settled'

const promises = [
  fetchSomething(),
  fetchSomethingThatWillFail(),
  fetchSomethingElse(),
]

whenAllSettled(promises).then(results => {
  // results contains [<Response>, undefined, <Response>]
})

API

whenAllSettled(promises, options)

Arguments:

  • promises: an array of Promises to wait for.
  • options (optional):
    • options.onRejection (optional): A rejection handler attached to each of the promises. The default logs the error to the console, unless process.env.NODE_ENV==='production'.

      Note: if this function throws an error, the returned promise rejects, and if it returns a value, that value will end up in the array of results.

Implementation

It's really just giving a more meaningful name to this one line of code:

promises.map(p => Promise.resolve(p).catch(onRejection))

when-all-settled's People

Contributors

reficul31 avatar treora avatar

Watchers

 avatar  avatar

Forkers

gozala

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.