New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

promise-handy-extension

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-handy-extension

Some handy extension for es6 promise such as Promise.reduce, Promise.series.

1.1.1
latest
Source
npm
Version published
Maintainers
1
Created
Source

Promise-Handy-Extension

NPM

Promise-Handy-Extension is a pretty simple ES6 promise extension. You can use it in node/browser ENV which has implemented Promises/A+ specification.

Build Status

Installation

$ npm install promise-handy-extension

Usage

How to import:

commonJS:

require ('promise-handy-extension');

ES6:

// ES6:
import 'promise-handy-extension';

Use it directly in browse:

<script type="text/javascript" src="../release/promiseHandyExtension.min.js"></script>
...
</script>

Then the Promise object will be extended with these methods:

Promise.series
Promise.reduce
...

How to use:

Promise.series
// an asyncCall:
function asyncCall(input) {
	// which will return a promise;
	return Promise.resolve(input);
}

Promise.series([input1, input2, input3], function(input, index) {
	return asyncCall(input);
}).then((results)=>{
	console.log(results);
	// it should be [input1, input2, input3];
});

The key point here for this method is the execution order is guaranteed which means only when the returned promise asyncCall(input1) is resolved, we will execute asyncCall(input2) to get the next promise to execute. This is why it called 'series'.

If one promise is rejected, it will fail fast: Promise.series will reject immediately.

For more detailed example, please check the test case which shows how this handy method can control your promise execute flow:

Promise.reduce

// an asyncCall:
function asyncCall(input) {
	// which will return a promise;
	return Promise.resolve(input);
}

Promise.reduce([input1, input2, input3], function(prev, curr, currIdx, arr) {
	return asyncCall(current).then(val=> prev + val);
}, 0).then((results)=>{
	console.log(results);
	// it should be 0 + input1 + input2 + input3;
});

The key point here for this method is the execution order is guaranteed which means only when the returned promise asyncCall(input1) is resolved, we will execute asyncCall(input2) to get the next promise to execute in reduce way.

If one promise is rejected, it will fail fast: Promise.reduce will reject immediately.

For more detailed example, please check the test case which shows how this handy method can control your promise execute flow:

License

MIT

Keywords

Promise.reduce

FAQs

Package last updated on 03 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts