1.2.1 • Published 8 years ago

reduce-for-promises v1.2.1

Weekly downloads
273
License
MIT
Repository
github
Last release
8 years ago

This is a simple implementation of Promise.reduce, that is available in Bluebird

Installation

npm install reduce-for-promises

Usage

var reduce = require('reduce-for-promises');

Example

Callback function will iterate to next item only after previous item's promise is resolved

reduce([1, 2, 3, 4], function (acc, item) {
	return new Promise(function (resolve) {
		setTimeout(function () {
			resolve(acc + item);
		}, 1000);
	});
}, 0).then(function (result) {
	console.log(result); // 10
});

You can set fourth argument to reduce if you want to set step size. For example, if you want two items to be processed at a time, here is the example

var delays = [1000, 100, 300, 200, 100];
reduce([1, 2, 3, 4, 5], function (acc, item, i) {
	return new Promise(function (resolve) {
		setTimeout(function () {
		    acc.sum += item;
			resolve(acc);
		}, delays[i]);
	});
}, {sum: 0}, 2).then(function (result) {
	console.log(result.sum); // 15
});

Please note that if you set step argument, make sure to use object as accumulator. Otherwise it won't be carried properly between callback iterations

1.2.1

8 years ago

1.2.0

8 years ago

1.1.1

8 years ago

1.1.0

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago