1.0.23 • Published 7 years ago

promise-foreach v1.0.23

Weekly downloads
169
License
MIT
Repository
github
Last release
7 years ago

promise-foreach

This is a simple implementation of foreach based on promises.

Works great in the browser with browserify.

Installation

$ npm install promise-foreach

Usage

Simple case
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
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)
    })
  })
}
Concurrency case
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

<script src="modules.js"></script>

<script>
  const promiseForeach = require('promise-foreach');
</script>

<script>
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)
      })
    })
  }
</script>

Tests

To run the package's test, first install the dependencies, then run npm test:

$ npm install
$ npm test

License

MIT License

1.0.23

7 years ago

1.0.22

7 years ago

1.0.21

7 years ago

1.0.20

7 years ago

1.0.19

7 years ago

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago