promise-sequential
Version:
Simple like Promise.all(), but sequentially!
69 lines (51 loc) • 1.65 kB
Markdown
//badge.fury.io/js/promise-sequential.png)](http://badge.fury.io/js/promise-sequential)
[](https://nodei.co/npm/promise-sequential.png?downloads=true&stars=true)
```
npm install --save promise-sequential
```
```js
const sequential = require('promise-sequential');
const items = [
() => new Promise( ... )
() => new Promise( ... )
() => new Promise( ... )
];
sequential(items)
.then(res => {
// ...
})
.catch(err => {
// ...
})
```
There is only one difference between Promise.all usage and promise-sequential usage: promise-sequential receive an Array of functions that each returns a promise.
Each function brings three params:
| Name | Description |
| ----------------- | ------------------------------------------------------------ |
| previousResponse | The response of previous iteration |
| responses | All responses received at the time |
| count | The current count. |
```js
const sequential = require('promise-sequential');
const array = [1,2,3,4,5];
sequential(array.map((item) => {
return function(previousResponse, responses, count) {
return new Promise(resolve => {
setTimeout(() => {
resolve(item)
}, 1000)
})
}
}))
.then(res => {
// ...
})
.catch(err => {
// ...
})
```
Simple like Promise.all(), but sequentially!
[![NPM version](https: