Giter VIP home page Giter VIP logo

p-reflect's Introduction

p-reflect Build Status

Make a promise always fulfill with its actual fulfillment value or rejection reason

Useful when you want a promise to fulfill no matter what and would rather handle the actual state afterwards.

Install

$ npm install --save p-reflect

Usage

Here, Promise.all would normally fail early because one of the promises rejects, but by using p-reflect, we can ignore the rejection and handle it later on.

const pReflect = require('p-reflect');

const promises = [
	getPromise(),
	getPromiseThatRejects(),
	getPromise()
];

Promise.all(promises.map(pReflect)).then(result => {
	console.log(result);
	/*
	[{
		isFulfilled: true,
		isRejected: false,
		value: 'πŸ¦„'
	},
	{
		isFulfilled: false,
		isRejected: true,
		reason: [Error: πŸ‘Ή]
	},
	{
		isFulfilled: true,
		isRejected: false,
		value: '🐴'
	}]
	*/

	const ret = f.filter(x => x.isFulfilled).map(x => x.value).join('');
	console.log(ret);
	//=> 'πŸ¦„πŸ΄'
});

The above is just an example. Use p-settle if you need this.

API

pReflect(input)

Returns a fulfilled Promise.

The fulfilled value is an object with the following properties:

  • isFulfilled
  • isRejected
  • value or reason (Depending on whether the promise fulfilled or rejected)

input

Type: Promise

Related

  • p-settle - Settle promises concurrently and get their fulfillment value or rejection reason
  • More…

License

MIT Β© Sindre Sorhus

p-reflect's People

Contributors

jhonnymoreira avatar sindresorhus 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.