Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 73183fa
Showing
9 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[{package.json,*.yml}] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* text=auto | ||
*.js text eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- '6' | ||
- '4' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
const pReduce = require('p-reduce'); | ||
|
||
module.exports = (iterable, iterator) => { | ||
const ret = []; | ||
|
||
return pReduce(iterable, (a, b, i) => { | ||
return Promise.resolve(iterator(b, i)).then(val => { | ||
ret.push(val); | ||
}); | ||
}).then(() => ret); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "p-map-series", | ||
"version": "0.0.0", | ||
"description": "Map over promises serially", | ||
"license": "MIT", | ||
"repository": "sindresorhus/p-map-series", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"promise", | ||
"map", | ||
"collection", | ||
"iterable", | ||
"iterator", | ||
"fulfilled", | ||
"serial", | ||
"serially", | ||
"async", | ||
"await", | ||
"promises", | ||
"bluebird" | ||
], | ||
"dependencies": { | ||
"p-reduce": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"delay": "^1.3.1", | ||
"time-span": "^1.0.0", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# p-map-series [![Build Status](https://travis-ci.org/sindresorhus/p-map-series.svg?branch=master)](https://travis-ci.org/sindresorhus/p-map-series) | ||
|
||
> Map over promises serially | ||
Useful as a side-effect mapper. Use [`p-map`](https://github.com/sindresorhus/p-map) if you don't need side-effects, as it's concurrent. | ||
|
||
|
||
## Install | ||
|
||
``` | ||
$ npm install --save p-map-series | ||
``` | ||
|
||
|
||
## Usage | ||
|
||
```js | ||
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](https://github.com/sindresorhus/p-each-series) - Iterate over promises serially | ||
- [p-reduce](https://github.com/sindresorhus/p-reduce) - Reduce a list of values using promises into a promise for a value | ||
- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently | ||
- [More…](https://github.com/sindresorhus/promise-fun) | ||
|
||
|
||
## License | ||
|
||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import test from 'ava'; | ||
import delay from 'delay'; | ||
import timeSpan from 'time-span'; | ||
import m from './'; | ||
|
||
const fixtureErr = new Error('fixture'); | ||
|
||
test('main', async t => { | ||
let index = 0; | ||
const ms = 100; | ||
const end = timeSpan(); | ||
const input = [Promise.resolve(1), 2, 3, Promise.resolve(4)]; | ||
|
||
const val = await m(input, async (x, i) => { | ||
t.is(x, i + 1); | ||
t.is(index, i); | ||
index++; | ||
await delay(ms); | ||
return x * 10; | ||
}); | ||
|
||
t.deepEqual(val, [10, 20, 30, 40]); | ||
t.true(end() > (ms - 20)); | ||
}); | ||
|
||
test('rejection input rejects the promise', async t => { | ||
t.throws(m([1, Promise.reject(fixtureErr)], () => {}), fixtureErr.message); | ||
t.throws(m([1, Promise.resolve(2)], () => Promise.reject(fixtureErr)), fixtureErr.message); | ||
}); | ||
|
||
test('handles empty iterable', async t => { | ||
t.deepEqual(await m([]), []); | ||
}); |