Giter VIP home page Giter VIP logo

promise-results's Introduction

promise-results

Promise all results of a collection of promises whether they are resolved OR rejected.

npm package build status dependency status

Often we want to accomplish several asyncrounous tasks at once. Usually, Promise.all does what we want, but it has a couple limitations:

  1. Rejections only provide rejection reason. Sometimes you want the data from Promises that did resolve, or you want to know specifically which items failed and which succeeded.
  2. Requires a numerically indexed array. It's often more conveniant to label our parralel jobs with object keys than a flat index. promise-results will handle keyed objects or arrays.

Example

var resultsOf = require('promise-results');
var pack = {};
pack.a = Promise.resolve(2);
pack.b = pack.a.then(function (r) {
  return new Promise(function (resolve, reject) {
    setTimeout(function() {
      resolve(r + 1);
    }, 10);
  })
});
pack.c = 42
pack.d = new Promise(function (resolve, reject) {
	setTimeout(function() {
	  reject(new Error('Some random failure'));
	}, 10);
});

resultsOf(pack).then(function (results) {
  assert(results.a === 2);
  assert(results.b === 3);
  assert(results.c === 42);
  assert(results.d instanceof Error);
});

allKeys

If you just want to use objects, but want to keep Promise.all single rejection pattern, you can use the allKeys function instead.

Example

var allKeys = require('promise-results/allKeys');
var pack = {};
pack.a = Promise.resolve(2);
pack.b = a.then(function (r) {
  return new Promise(funciton (resolve, reject) {
    setTimeout(function() {
      resolve(r + 1);
    }, 10);
  })
});
pack.c = 42
pack.d = new Promise(function (resolve, reject) {
	setTimeout(function() {
	  reject(new Error('Some random failure'));
	}, 10);
});

allKeys(pack).catch(function (err) {
  assert(err instanceof Error);
});

resultSet

If you like having a defined reject route, but still want access to your partial results and rejections. You can use resultSet.

Example

var results = require('promise-results/resultSet');
var pack = {};
pack.a = Promise.resolve(2);
pack.b = a.then(function (r) {
  return new Promise(funciton (resolve, reject) {
    setTimeout(function() {
      resolve(r + 1);
    }, 10);
  })
});
pack.c = 42
pack.d = new Promise(function (resolve, reject) {
  setTimeout(function() {
    reject(new Error('Some random failure'));
  }, 10);
});

results(pack).catch(function (result) {
  assert(result.d instanceof Error);
});

promise-results's People

Contributors

robcolburn avatar sidneynemzer avatar

Watchers

 avatar James Cloos 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.