https://github.com/saulsluz/promise-foreach
This is a simple implementation of foreach based on promises.
https://github.com/saulsluz/promise-foreach
foreach nodejs promise
Last synced: 8 months ago
JSON representation
This is a simple implementation of foreach based on promises.
- Host: GitHub
- URL: https://github.com/saulsluz/promise-foreach
- Owner: saulsluz
- License: mit
- Created: 2017-06-23T16:04:20.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2019-11-28T23:17:25.000Z (over 6 years ago)
- Last Synced: 2025-09-24T03:34:26.106Z (9 months ago)
- Topics: foreach, nodejs, promise
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# promise-foreach
Very simple and useful implementation of foreach based on promises for parallel requests. For NodeJS.
Works great in the browser with [browserify](http://github.com/substack/node-browserify).
### Installation
`$ npm install promise-foreach`
### Usage
###### Simple case
```javascript
const promiseForeach = require('promise-foreach')
const list = [{
firstName: 'John',
lastName: 'Doe'
}, {
firstName: 'Marie',
lastName: 'Doe'
}]
promiseForeach.each(list,
person => {
return `${person.firstName} ${person.lastName}`
},
(arrResult, person) => {
return {
firstName: person.firstName,
lastName: person.lastName,
fullName: arrResult[0]
}
},
(err, newList) => {
if (err) {
console.error(err)
return ;
}
console.log('newList : ', newList)
})
```
###### Complex case
```javascript
const https = require('https');
const promiseForeach = require('promise-foreach')
const list = [{
firstName: 'John',
lastName: 'Doe',
photo_id: 1
}, {
firstName: 'Marie',
lastName: 'Doe',
photo_id: 2
}]
promiseForeach.each(list,
[
person => {
return `${person.firstName} ${person.lastName}`
},
person => {
return asyncGetPhoto(person.photo_id)
}
],
(arrResult, person) => {
return {
firstName: person.firstName,
lastName: person.lastName,
fullName: arrResult[0],
photo: arrResult[1]
}
},
(err, newList) => {
if (err) {
console.error(err)
return ;
}
console.log('newList : ', newList)
})
function asyncGetPhoto(photo_id) {
return new Promise(function (resolve, reject) {
var request = https.get('https://jsonplaceholder.typicode.com/photos/' + photo_id, function (response) {
var body = []
response.on('data', function (chunk) {
body.push(chunk)
})
response.on('end', function () {
resolve(JSON.parse(body.join('')))
})
})
request.on('error', function (err) {
reject(err)
})
})
}
```
###### Parallel case
```javascript
const https = require('https');
const promiseForeach = require('promise-foreach')
const list = [{
firstName: 'John',
lastName: 'Doe',
photo_id: 1,
comment_id: 3
}, {
firstName: 'Marie',
lastName: 'Doe',
photo_id: 2,
comment_id: 4
}]
promiseForeach.each(list,
[
person => {
return `${person.firstName} ${person.lastName}`
},
person => {
return asyncGetPhoto(person.photo_id)
},
person => {
return asyncGetComment(person.comment_id)
}
],
(arrResult, person) => {
return {
firstName: person.firstName,
lastName: person.lastName,
fullName: arrResult[0],
photo: arrResult[1],
comment: arrResult[2]
}
},
(err, newList) => {
if (err) {
console.error(err)
return ;
}
console.log('newList : ', newList)
})
function asyncGetPhoto(photo_id) {
return new Promise(function (resolve, reject) {
var request = https.get('https://jsonplaceholder.typicode.com/photos/' + photo_id, function (response) {
var body = []
response.on('data', function (chunk) {
body.push(chunk)
})
response.on('end', function () {
setTimeout(function () {
resolve(JSON.parse(body.join('')))
}, 1000)
})
})
request.on('error', function (err) {
reject(err)
})
})
}
function asyncGetComment(comment_id) {
return new Promise(function (resolve, reject) {
var request = https.get('https://jsonplaceholder.typicode.com/comments/' + comment_id, function (response) {
var body = []
response.on('data', function (chunk) {
body.push(chunk)
})
response.on('end', function () {
resolve(JSON.parse(body.join('')))
})
})
request.on('error', function (err) {
reject(err)
})
})
}
```
###### Browser case
`$ browserify -r promise-foreach > modules.js`
```html
const promiseForeach = require('promise-foreach');
const list = [{
firstName: 'John',
lastName: 'Doe',
photo_id: 1
}, {
firstName: 'Marie',
lastName: 'Doe',
photo_id: 2
}]
promiseForeach.each(list,
[
person => {
return `${person.firstName} ${person.lastName}`
},
person => {
return asyncGetPhoto(person.photo_id)
}
],
(arrResult, person) => {
return {
firstName: person.firstName,
lastName: person.lastName,
fullName: arrResult[0],
photo: arrResult[1]
}
},
(err, newList) => {
if (err) {
console.error(err)
return ;
}
console.log('newList : ', newList)
})
function asyncGetPhoto(photo_id){
return new Promise(function(resolve, reject){
var request = new Request('https://jsonplaceholder.typicode.com/photos/' + photo_id,{
method: 'GET'
})
fetch(request).then(function(response){
resolve(response.json())
})
.catch(function(error){
reject(error)
})
})
}
```
### Tests
To run the package's test, first install the dependencies, then run `npm test`:
```
$ npm install
$ npm test
```
### License
MIT License