Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Starefossen/node-async-each-map

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Async; Each, Map, Reduce

Build status Codacy grade Codacy coverage 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.

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`);
  }
});

About

🐢 🚀 Dependency free async each, map, and reduce written in ES6 package for Node.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published