async-each-map

1.0.2 • Public • Published

Async; Each, Map, Reduce

Build status Codacy NPM downloads NPM version Node version Dependency status

Efficient, light wegith, dependency free NPM package for doing asyncronous each, map, and reduce operations in Node.js.

Requirements

  • Node.JS >= v4.0.0

Install

$ npm install async-each-map --save

Usage

const each = require('async-each-map');

each(array, iterator, [callback])

Applies the iterator function to each item in array, in sequence and in order. The iterator is called with an item from the list, and a next function for when it has finished. If the iterator passes an error to the next function, the callback is immediately called with the error.

Arguments

  • array - An array to iterate over.
  • iterator(item, next) - A function to apply to each item in arr. The iterator is passed a callback(err) which must be called once it has completed. If no error has occurred, the callback should be run without arguments or with an explicit null argument. The array index is not passed to the iterator.
  • callback(error, array) - Optional A callback which is called when all iterator functions have finished, or an error occurs. When using the map functionality a second parameter is provided.

Examples

each([3, 6, 2, 9], (item, next) => {
  some.async.operation(item, error => {
    next(error);
  });
}, error => {
  if (error) { throw error; }
 
  console.log('Done!');
});

Map

each(['/some/file1', '/some/file2', '/some/file3'], fs.readFile, (error, files) => {
  if (error) { throw error; }
 
  for (const content in files) {
    console.log(content);
  }
})

Reduce

each(['/some/file1', '/some/file2', '/some/file3'], fs.exists, (error, files) => {
  if (error) { throw error; }
 
  for (const file in files) {
    console.log(`${file} exists`);
  }
});

MIT Licensed

Package Sidebar

Install

npm i async-each-map

Weekly Downloads

9

Version

1.0.2

License

MIT

Last publish

Collaborators

  • starefossen