What is async-each?
The async-each package is a small, zero-dependency utility module for Node.js that provides a simple way to iterate over an array asynchronously. It allows you to apply a function to each item in an array in parallel, without waiting for the previous function call to complete, making it useful for performing IO-bound tasks or operations that are CPU-intensive but can be run concurrently.
What are async-each's main functionalities?
Asynchronous iteration over an array
This feature allows you to asynchronously process each item in an array. The provided code sample demonstrates how async-each can be used to log each item in an array with a delay of 1 second between each log, simulating an asynchronous operation like reading files or making HTTP requests.
const asyncEach = require('async-each');
const myArray = [1, 2, 3, 4];
asyncEach(myArray, (item, callback) => {
setTimeout(() => {
console.log(item);
callback();
}, 1000);
}, (err) => {
if (err) throw err;
console.log('All items have been processed.');
});
Other packages similar to async-each
async
The 'async' package is a comprehensive collection of utility functions for working with asynchronous JavaScript. It offers much more than just asynchronous iteration, including control flow, collection manipulation, and utility functions. Compared to async-each, it is more feature-rich but also larger in size, making async-each a lightweight alternative for projects that only require asynchronous iteration.
bluebird
Bluebird is a powerful promise library that, among many other features, offers utilities for concurrent execution of promises, which can be used for asynchronous iteration similar to async-each. Bluebird is known for its performance and extensive feature set. It is a good choice if you are already working with promises and need a broad set of features beyond asynchronous iteration.
p-each-series
p-each-series is a package that allows for serial iteration over an array of items with promises. Unlike async-each, which performs iterations in parallel, p-each-series ensures that each item is processed one after the other, waiting for the current promise to resolve before moving to the next. This is useful for tasks that must be done in order but still benefit from asynchronous execution.
async-each
No-bullshit, ultra-simple, 35-lines-of-code async parallel forEach function for JavaScript.
We don't need junky 30K async libs. Really.
For browsers and node.js.
Installation
- Just include async-each before your scripts.
npm install async-each
if you’re using node.js.bower install async-each
if you’re using Bower.
Usage
each(array, iterator, callback);
— Array
, Function
, (optional) Function
iterator(item, next)
receives current item and a callback that will mark the item as done. next
callback receives optional error, transformedItem
arguments.callback(error, transformedArray)
optionally receives first error and transformed result Array
.
Node.js:
var each = require('async-each');
each(['a.js', 'b.js', 'c.js'], fs.readFile, function(error, contents) {
if (error) console.error(error);
console.log('Contents for a, b and c:', contents);
});
Browser:
window.asyncEach(list, fn, callback);
License
The MIT License