What is each-async?
The each-async npm package allows you to asynchronously iterate over an array, performing asynchronous operations on each item in the array. It is useful for scenarios where you need to handle asynchronous tasks in a sequential manner.
What are each-async's main functionalities?
Asynchronous Iteration
This feature allows you to iterate over an array and perform asynchronous operations on each item. The `asyncOperation` function is called for each item in the array, and the `done` callback is called when the operation is complete. The final callback is called when all items have been processed.
const eachAsync = require('each-async');
const items = [1, 2, 3, 4, 5];
function asyncOperation(item, index, done) {
setTimeout(() => {
console.log('Processing item:', item);
done();
}, 1000);
}
eachAsync(items, asyncOperation, (err) => {
if (err) {
console.error('Error:', err);
} else {
console.log('All items have been processed.');
}
});
Error Handling
This feature demonstrates how to handle errors during the asynchronous iteration. If an error occurs during the processing of an item, the `done` callback is called with an error object, and the final callback receives the error.
const eachAsync = require('each-async');
const items = [1, 2, 3, 4, 5];
function asyncOperation(item, index, done) {
setTimeout(() => {
if (item === 3) {
done(new Error('An error occurred with item 3'));
} else {
console.log('Processing item:', item);
done();
}
}, 1000);
}
eachAsync(items, asyncOperation, (err) => {
if (err) {
console.error('Error:', err);
} else {
console.log('All items have been processed.');
}
});
Other packages similar to each-async
async
The async package provides a wide range of utilities for working with asynchronous JavaScript. It includes functions for parallel and sequential execution, as well as error handling and control flow. Compared to each-async, async offers a more comprehensive set of tools for managing asynchronous operations.
bluebird
Bluebird is a fully-featured Promise library for JavaScript. It provides powerful tools for managing asynchronous code, including methods for iterating over arrays with promises. Bluebird's `Promise.each` method offers similar functionality to each-async but with the added benefits of promise-based syntax and additional features.
p-map
The p-map package allows you to map over promises concurrently. It provides a simple and efficient way to handle asynchronous operations on arrays, with control over the concurrency level. Compared to each-async, p-map focuses on promise-based iteration and offers more control over concurrency.
each-async
Async concurrent iterator (async forEach)
Like async.each(), but tiny.
I often use async.each()
for doing async operations when iterating, but I almost never use the other gadzillion methods in async
.
Async iteration is one of the most used async control flow patterns.
I would strongly recommend using promises instead. You could then use the built-in Promise.all()
, or p-map
if you need concurrency control.
Install
$ npm install --save each-async
Usage
const eachAsync = require('each-async');
eachAsync(['foo','bar','baz'], (item, index, done) => {
console.log(item, index);
done();
}, error => {
console.log('finished');
});
API
eachAsync(input, callback, [finishedCallback])
input
Type: Array
Array you want to iterate.
callback(item, index, done)
Type: Function
Called for each item in the array with the following arguments:
item
: the current item in the arrayindex
: the current indexdone([error])
: call this when you're done with an optional error. Supplying anything other than undefined
/null
will stop the iteration.
Note that order is not guaranteed since each item is handled concurrently.
finishedCallback(error)
Type: Function
Called when the iteration is finished or on the first error. First argument is the error passed from done()
in the callback
.
License
MIT © Sindre Sorhus