Giter Site home page Giter Site logo

promises-least's Introduction

Promises-least

A simple util for handling promises.

What is it for?

If you have several promises and you need to do some actions when at least one of these promises resolves. You will receive collection of values and promises result.

Also, it has concurrent mode, so when one of your promises resolves or rejects, you will receive only this value and accordingly

Usage

Function receives array of promises and returns promise, that resolves when at least one of provided promises was resolved. If all were rejected - it will be rejected too.

Returns collection of values that promises were rejected/resoled and results of promise (resolved - true, rejected - true).

All values returns with respect of original array order, not promises resolving order.

In concurrent mode the first resulted promise is returned.

Default mode.

import promisesLeast from 'promises-least'

const promise1 = someRequest1()
const promise2 = somePromise2()
const promise3 = somePromise3()

promisesLeast([promise1, promise2, promise3])
  .then((collection) => {
    // make some things with resolved collection
  })
  .catch((collection) => {
    // make some with rejected one
  })

With async/await:

import promisesLeast from 'promises-least'

const promise1 = someRequest1() // resolves with 1
const promise2 = somePromise2() // rejects with 2
const promise3 = somePromise3() // rejects with 3

try {
  const collection = promisesLeast([promise1, promise2, promise3])

  /*
    {
      values: [1, 2, 3],
      results: [true, false, false]
    }
  */

  // make some things with resolved collection
} catch (collection) {
  // make some with rejected one
}

Concurrent mode

Results with first resolved or rejected promise

API

promises-least receives array of promises and option for concurrent mode.

interface PromisesLeast {
  iterable: Promise<any>[],
  concurrent?: boolean
}

Returns collection in default mode:

interface PromisesLeastResult {
  values: any[],
  results: boolean[]
}

In concurrent mode promise is resulting with just promise value.

TODO's

  • [] for concurrent mode add option for resulting with only first resolved/rejected promise

promises-least's People

Contributors

babkinaleksandr avatar

Watchers

 avatar

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.