Map over promises serially
Branch: master
Clone or download
Latest commit fbaef9e Oct 21, 2016
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig init Oct 21, 2016
.gitattributes init Oct 21, 2016
.gitignore init Oct 21, 2016
.travis.yml init Oct 21, 2016
index.js init Oct 21, 2016
license init Oct 21, 2016
package.json 1.0.0 Oct 21, 2016
readme.md init Oct 21, 2016
test.js init Oct 21, 2016

readme.md

p-map-series Build Status

Map over promises serially

Useful as a side-effect mapper. Use p-map if you don't need side-effects, as it's concurrent.

Install

$ npm install --save p-map-series

Usage

const pMapSeries = require('p-map-series');

const keywords = [
	getTopKeyword() //=> Promise
	'rainbow',
	'pony'
];

let scores = [];

const mapper = keyword => fetchScore(keyword).then(score => {
	scores.push(score);
	return {keyword, score};
});

pMapSeries(keywords, mapper).then(result => {
	console.log(result);
	/*
	[{
		keyword: 'unicorn',
		score: 99
	}, {
		keyword: 'rainbow',
		score: 70
	}, {
		keyword: 'pony',
		score: 79}
	]
	*/
});

API

pMapSeries(input, mapper)

Returns a Promise that is fulfilled when all promises in input and ones returned from mapper are fulfilled, or rejects if any of the promises reject. The fulfilled value is an Array of the mapper created promises fulfillment values.

input

Type: Iterable<Promise|any>

Mapped over serially in the mapper function.

mapper(element, index)

Type: Function

Expected to return a value. If it's a Promise, it's awaited before continuing with the next iteration.

Related

  • p-each-series - Iterate over promises serially
  • p-reduce - Reduce a list of values using promises into a promise for a value
  • p-map - Map over promises concurrently
  • More…

License

MIT © Sindre Sorhus