A collection of async functions for manipulating collections, such as
arrays and objects.
Methods
(static) concat(coll, iteratee, callback(err)opt)
import concat from 'async/concat';Applies iteratee to each item in coll, concatenating the results. Returns
the concatenated list. The iteratees are called in parallel, and the
results are concatenated as they return. There is no guarantee that the
results array will be returned in the original order of coll passed to the
iteratee function.
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | A function to apply to each item in |
callback(err) |
function <optional> | A callback which is called after all the
|
Example
async.concat(['dir1','dir2','dir3'], fs.readdir, function(err, files) {
// files is now a list of filenames that exist in the 3 directories
});
(static) concatLimit(coll, limit, iteratee, callbackopt)
import concatLimit from 'async/concatLimit';The same as concat but runs a maximum of limit async operations at a time.
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
limit |
number | The maximum number of async operations at a time. |
iteratee |
AsyncFunction | A function to apply to each item in |
callback |
function <optional> | A callback which is called after all the
|
- Source:
- See:
(static) concatSeries(coll, iteratee, callback(err)opt)
import concatSeries from 'async/concatSeries';The same as concat but runs only a single async operation at a time.
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | A function to apply to each item in |
callback(err) |
function <optional> | A callback which is called after all the
|
- Source:
- See:
(static) detect(coll, iteratee, callbackopt)
import detect from 'async/detect';Returns the first value in coll that passes an async truth test. The
iteratee is applied in parallel, meaning the first iteratee to return
true will fire the detect callback with that result. That means the
result might not be the first item in the original coll (in terms of order)
that passes the test.
If order within the original coll is important, then look at
detectSeries.
- Alias:
- find
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | A truth test to apply to each item in |
callback |
function <optional> | A callback which is called as soon as any
iteratee returns |
Example
async.detect(['file1','file2','file3'], function(filePath, callback) {
fs.access(filePath, function(err) {
callback(null, !err)
});
}, function(err, result) {
// result now equals the first file in the list that exists
});
(static) detectLimit(coll, limit, iteratee, callbackopt)
import detectLimit from 'async/detectLimit';The same as detect but runs a maximum of limit async operations at a
time.
- Alias:
- findLimit
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
limit |
number | The maximum number of async operations at a time. |
iteratee |
AsyncFunction | A truth test to apply to each item in |
callback |
function <optional> | A callback which is called as soon as any
iteratee returns |
- Source:
- See:
(static) detectSeries(coll, iteratee, callbackopt)
import detectSeries from 'async/detectSeries';The same as detect but runs only a single async operation at a time.
- Alias:
- findSeries
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | A truth test to apply to each item in |
callback |
function <optional> | A callback which is called as soon as any
iteratee returns |
- Source:
- See:
(static) each(coll, iteratee, callbackopt)
import each from 'async/each';Applies the function iteratee to each item in coll, in parallel.
The iteratee is called with an item from the list, and a callback for when
it has finished. If the iteratee passes an error to its callback, the
main callback (for the each function) is immediately called with the
error.
Note, that since this function applies iteratee to each item in parallel,
there is no guarantee that the iteratee functions will complete in order.
- Alias:
- forEach
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | An async function to apply to
each item in |
callback |
function <optional> | A callback which is called when all
|
Example
// assuming openFiles is an array of file names and saveFile is a function
// to save the modified contents of that file:
async.each(openFiles, saveFile, function(err){
// if any of the saves produced an error, err would equal that error
});
// assuming openFiles is an array of file names
async.each(openFiles, function(file, callback) {
// Perform operation on file here.
console.log('Processing file ' + file);
if( file.length > 32 ) {
console.log('This file name is too long');
callback('File name too long');
} else {
// Do work to process file here
console.log('File processed');
callback();
}
}, function(err) {
// if any of the file processing produced an error, err would equal that error
if( err ) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
}
});
(static) eachLimit(coll, limit, iteratee, callbackopt)
import eachLimit from 'async/eachLimit';The same as each but runs a maximum of limit async operations at a time.
- Alias:
- forEachLimit
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
limit |
number | The maximum number of async operations at a time. |
iteratee |
AsyncFunction | An async function to apply to each item in
|
callback |
function <optional> | A callback which is called when all
|
- Source:
- See:
(static) eachOf(coll, iteratee, callbackopt)
import eachOf from 'async/eachOf';Like each, except that it passes the key (or index) as the second argument
to the iteratee.
- Alias:
- forEachOf
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | A function to apply to each
item in |
callback |
function <optional> | A callback which is called when all
|
Example
var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
var configs = {};
async.forEachOf(obj, function (value, key, callback) {
fs.readFile(__dirname + value, "utf8", function (err, data) {
if (err) return callback(err);
try {
configs[key] = JSON.parse(data);
} catch (e) {
return callback(e);
}
callback();
});
}, function (err) {
if (err) console.error(err.message);
// configs is now a map of JSON data
doSomethingWith(configs);
});
- Source:
- See:
(static) eachOfLimit(coll, limit, iteratee, callbackopt)
import eachOfLimit from 'async/eachOfLimit';The same as eachOf but runs a maximum of limit async operations at a
time.
- Alias:
- forEachOfLimit
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
limit |
number | The maximum number of async operations at a time. |
iteratee |
AsyncFunction | An async function to apply to each
item in |
callback |
function <optional> | A callback which is called when all
|
- Source:
- See:
(static) eachOfSeries(coll, iteratee, callbackopt)
import eachOfSeries from 'async/eachOfSeries';The same as eachOf but runs only a single async operation at a time.
- Alias:
- forEachOfSeries
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | An async function to apply to each item in
|
callback |
function <optional> | A callback which is called when all |
- Source:
- See:
(static) eachSeries(coll, iteratee, callbackopt)
import eachSeries from 'async/eachSeries';The same as each but runs only a single async operation at a time.
- Alias:
- forEachSeries
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | An async function to apply to each
item in |
callback |
function <optional> | A callback which is called when all
|
- Source:
- See:
(static) every(coll, iteratee, callbackopt)
import every from 'async/every';Returns true if every element in coll satisfies an async test. If any
iteratee call returns false, the main callback is immediately called.
- Alias:
- all
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | An async truth test to apply to each item in the collection in parallel. The iteratee must complete with a boolean result value. Invoked with (item, callback). |
callback |
function <optional> | A callback which is called after all the
|
Example
async.every(['file1','file2','file3'], function(filePath, callback) {
fs.access(filePath, function(err) {
callback(null, !err)
});
}, function(err, result) {
// if result is true then every file exists
});
(static) everyLimit(coll, limit, iteratee, callbackopt)
import everyLimit from 'async/everyLimit';The same as every but runs a maximum of limit async operations at a time.
- Alias:
- allLimit
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
limit |
number | The maximum number of async operations at a time. |
iteratee |
AsyncFunction | An async truth test to apply to each item in the collection in parallel. The iteratee must complete with a boolean result value. Invoked with (item, callback). |
callback |
function <optional> | A callback which is called after all the
|
- Source:
- See:
(static) everySeries(coll, iteratee, callbackopt)
import everySeries from 'async/everySeries';The same as every but runs only a single async operation at a time.
- Alias:
- allSeries
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | An async truth test to apply to each item in the collection in series. The iteratee must complete with a boolean result value. Invoked with (item, callback). |
callback |
function <optional> | A callback which is called after all the
|
- Source:
- See:
(static) filter(coll, iteratee, callbackopt)
import filter from 'async/filter';Returns a new array of all the values in coll which pass an async truth
test. This operation is performed in parallel, but the results array will be
in the same order as the original.
- Alias:
- select
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
function | A truth test to apply to each item in |
callback |
function <optional> | A callback which is called after all the
|
Example
async.filter(['file1','file2','file3'], function(filePath, callback) {
fs.access(filePath, function(err) {
callback(null, !err)
});
}, function(err, results) {
// results now equals an array of the existing files
});
(static) filterLimit(coll, limit, iteratee, callbackopt)
import filterLimit from 'async/filterLimit';The same as filter but runs a maximum of limit async operations at a
time.
- Alias:
- selectLimit
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
limit |
number | The maximum number of async operations at a time. |
iteratee |
function | A truth test to apply to each item in |
callback |
function <optional> | A callback which is called after all the
|
- Source:
- See:
(static) filterSeries(coll, iteratee, callbackopt)
import filterSeries from 'async/filterSeries';The same as filter but runs only a single async operation at a time.
- Alias:
- selectSeries
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
function | A truth test to apply to each item in |
callback |
function <optional> | A callback which is called after all the
|
- Source:
- See:
(static) groupBy(coll, iteratee, callbackopt)
import groupBy from 'async/groupBy';Returns a new object, where each value corresponds to an array of items, from
coll, that returned the corresponding key. That is, the keys of the object
correspond to the values passed to the iteratee callback.
Note: Since this function applies the iteratee to each item in parallel,
there is no guarantee that the iteratee functions will complete in order.
However, the values for each key in the result will be in the same order as
the original coll. For Objects, the values will roughly be in the order of
the original Objects' keys (but this can vary across JavaScript engines).
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | An async function to apply to each item in
|
callback |
function <optional> | A callback which is called when all |
Example
async.groupBy(['userId1', 'userId2', 'userId3'], function(userId, callback) {
db.findById(userId, function(err, user) {
if (err) return callback(err);
return callback(null, user.age);
});
}, function(err, result) {
// result is object containing the userIds grouped by age
// e.g. { 30: ['userId1', 'userId3'], 42: ['userId2']};
});
- Source:
(static) groupByLimit(coll, limit, iteratee, callbackopt)
import groupByLimit from 'async/groupByLimit';The same as groupBy but runs a maximum of limit async operations at a time.
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
limit |
number | The maximum number of async operations at a time. |
iteratee |
AsyncFunction | An async function to apply to each item in
|
callback |
function <optional> | A callback which is called when all |
- Source:
- See:
(static) groupBySeries(coll, limit, iteratee, callbackopt)
import groupBySeries from 'async/groupBySeries';The same as groupBy but runs only a single async operation at a time.
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
limit |
number | The maximum number of async operations at a time. |
iteratee |
AsyncFunction | An async function to apply to each item in
|
callback |
function <optional> | A callback which is called when all |
- Source:
- See:
(static) map(coll, iteratee, callbackopt)
import map from 'async/map';Produces a new collection of values by mapping each value in coll through
the iteratee function. The iteratee is called with an item from coll
and a callback for when it has finished processing. Each of these callback
takes 2 arguments: an error, and the transformed item from coll. If
iteratee passes an error to its callback, the main callback (for the
map function) is immediately called with the error.
Note, that since this function applies the iteratee to each item in
parallel, there is no guarantee that the iteratee functions will complete
in order. However, the results array will be in the same order as the
original coll.
If map is passed an Object, the results will be an Array. The results
will roughly be in the order of the original Objects' keys (but this can
vary across JavaScript engines).
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | An async function to apply to each item in
|
callback |
function <optional> | A callback which is called when all |
Example
async.map(['file1','file2','file3'], fs.stat, function(err, results) {
// results is now an array of stats for each file
});
(static) mapLimit(coll, limit, iteratee, callbackopt)
import mapLimit from 'async/mapLimit';The same as map but runs a maximum of limit async operations at a time.
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
limit |
number | The maximum number of async operations at a time. |
iteratee |
AsyncFunction | An async function to apply to each item in
|
callback |
function <optional> | A callback which is called when all |
- Source:
- See:
(static) mapSeries(coll, iteratee, callbackopt)
import mapSeries from 'async/mapSeries';The same as map but runs only a single async operation at a time.
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | An async function to apply to each item in
|
callback |
function <optional> | A callback which is called when all |
- Source:
- See:
(static) mapValues(obj, iteratee, callbackopt)
import mapValues from 'async/mapValues';A relative of map, designed for use with objects.
Produces a new Object by mapping each value of obj through the iteratee
function. The iteratee is called each value and key from obj and a
callback for when it has finished processing. Each of these callbacks takes
two arguments: an error, and the transformed item from obj. If iteratee
passes an error to its callback, the main callback (for the mapValues
function) is immediately called with the error.
Note, the order of the keys in the result is not guaranteed. The keys will be roughly in the order they complete, (but this is very engine-specific)
Parameters:
| Name | Type | Description |
|---|---|---|
obj |
Object | A collection to iterate over. |
iteratee |
AsyncFunction | A function to apply to each value and key
in |
callback |
function <optional> | A callback which is called when all |
Example
async.mapValues({
f1: 'file1',
f2: 'file2',
f3: 'file3'
}, function (file, key, callback) {
fs.stat(file, callback);
}, function(err, result) {
// result is now a map of stats for each file, e.g.
// {
// f1: [stats for file1],
// f2: [stats for file2],
// f3: [stats for file3]
// }
});
- Source:
(static) mapValuesLimit(obj, limit, iteratee, callbackopt)
import mapValuesLimit from 'async/mapValuesLimit';The same as mapValues but runs a maximum of limit async operations at a
time.
Parameters:
| Name | Type | Description |
|---|---|---|
obj |
Object | A collection to iterate over. |
limit |
number | The maximum number of async operations at a time. |
iteratee |
AsyncFunction | A function to apply to each value and key
in |
callback |
function <optional> | A callback which is called when all |
- Source:
- See:
(static) mapValuesSeries(obj, iteratee, callbackopt)
import mapValuesSeries from 'async/mapValuesSeries';The same as mapValues but runs only a single async operation at a time.
Parameters:
| Name | Type | Description |
|---|---|---|
obj |
Object | A collection to iterate over. |
iteratee |
AsyncFunction | A function to apply to each value and key
in |
callback |
function <optional> | A callback which is called when all |
- Source:
- See:
(static) reduce(coll, memo, iteratee, callbackopt)
import reduce from 'async/reduce';Reduces coll into a single value using an async iteratee to return each
successive step. memo is the initial state of the reduction. This function
only operates in series.
For performance reasons, it may make sense to split a call to this function
into a parallel map, and then use the normal Array.prototype.reduce on the
results. This function is for situations where each step in the reduction
needs to be async; if you can get the data before reducing it, then it's
probably a good idea to do so.
- Alias:
- foldl
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
memo |
* | The initial state of the reduction. |
iteratee |
AsyncFunction | A function applied to each item in the
array to produce the next step in the reduction.
The |
callback |
function <optional> | A callback which is called after all the
|
Example
async.reduce([1,2,3], 0, function(memo, item, callback) {
// pointless async:
process.nextTick(function() {
callback(null, memo + item)
});
}, function(err, result) {
// result is now equal to the last value of memo, which is 6
});
(static) reduceRight(array, memo, iteratee, callbackopt)
import reduceRight from 'async/reduceRight';Same as reduce, only operates on array in reverse order.
- Alias:
- foldr
Parameters:
| Name | Type | Description |
|---|---|---|
array |
Array | A collection to iterate over. |
memo |
* | The initial state of the reduction. |
iteratee |
AsyncFunction | A function applied to each item in the
array to produce the next step in the reduction.
The |
callback |
function <optional> | A callback which is called after all the
|
- Source:
- See:
(static) reject(coll, iteratee, callbackopt)
import reject from 'async/reject';The opposite of filter. Removes values that pass an async truth test.
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
function | An async truth test to apply to each item in
|
callback |
function <optional> | A callback which is called after all the
|
Example
async.reject(['file1','file2','file3'], function(filePath, callback) {
fs.access(filePath, function(err) {
callback(null, !err)
});
}, function(err, results) {
// results now equals an array of missing files
createFiles(results);
});
- Source:
- See:
(static) rejectLimit(coll, limit, iteratee, callbackopt)
import rejectLimit from 'async/rejectLimit';The same as reject but runs a maximum of limit async operations at a
time.
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
limit |
number | The maximum number of async operations at a time. |
iteratee |
function | An async truth test to apply to each item in
|
callback |
function <optional> | A callback which is called after all the
|
- Source:
- See:
(static) rejectSeries(coll, iteratee, callbackopt)
import rejectSeries from 'async/rejectSeries';The same as reject but runs only a single async operation at a time.
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
function | An async truth test to apply to each item in
|
callback |
function <optional> | A callback which is called after all the
|
- Source:
- See:
(static) some(coll, iteratee, callbackopt)
import some from 'async/some';Returns true if at least one element in the coll satisfies an async test.
If any iteratee call returns true, the main callback is immediately
called.
- Alias:
- any
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | An async truth test to apply to each item
in the collections in parallel.
The iteratee should complete with a boolean |
callback |
function <optional> | A callback which is called as soon as any
iteratee returns |
Example
async.some(['file1','file2','file3'], function(filePath, callback) {
fs.access(filePath, function(err) {
callback(null, !err)
});
}, function(err, result) {
// if result is true then at least one of the files exists
});
(static) someLimit(coll, limit, iteratee, callbackopt)
import someLimit from 'async/someLimit';The same as some but runs a maximum of limit async operations at a time.
- Alias:
- anyLimit
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
limit |
number | The maximum number of async operations at a time. |
iteratee |
AsyncFunction | An async truth test to apply to each item
in the collections in parallel.
The iteratee should complete with a boolean |
callback |
function <optional> | A callback which is called as soon as any
iteratee returns |
- Source:
- See:
(static) someSeries(coll, iteratee, callbackopt)
import someSeries from 'async/someSeries';The same as some but runs only a single async operation at a time.
- Alias:
- anySeries
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | An async truth test to apply to each item
in the collections in series.
The iteratee should complete with a boolean |
callback |
function <optional> | A callback which is called as soon as any
iteratee returns |
- Source:
- See:
(static) sortBy(coll, iteratee, callback)
import sortBy from 'async/sortBy';Sorts a list by the results of running each coll value through an async
iteratee.
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
iteratee |
AsyncFunction | An async function to apply to each item in
|
callback |
function | A callback which is called after all the
|
Example
async.sortBy(['file1','file2','file3'], function(file, callback) {
fs.stat(file, function(err, stats) {
callback(err, stats.mtime);
});
}, function(err, results) {
// results is now the original array of files sorted by
// modified date
});
// By modifying the callback parameter the
// sorting order can be influenced:
// ascending order
async.sortBy([1,9,3,5], function(x, callback) {
callback(null, x);
}, function(err,result) {
// result callback
});
// descending order
async.sortBy([1,9,3,5], function(x, callback) {
callback(null, x*-1); //<- x*-1 instead of x, turns the order around
}, function(err,result) {
// result callback
});
(static) transform(coll, accumulatoropt, iteratee, callbackopt)
import transform from 'async/transform';A relative of reduce. Takes an Object or Array, and iterates over each
element in series, each step potentially mutating an accumulator value.
The type of the accumulator defaults to the type of collection passed in.
Parameters:
| Name | Type | Description |
|---|---|---|
coll |
Array | Iterable | Object | A collection to iterate over. |
accumulator |
* <optional> | The initial state of the transform. If omitted,
it will default to an empty Object or Array, depending on the type of |
iteratee |
AsyncFunction | A function applied to each item in the collection that potentially modifies the accumulator. Invoked with (accumulator, item, key, callback). |
callback |
function <optional> | A callback which is called after all the
|
Examples
async.transform([1,2,3], function(acc, item, index, callback) {
// pointless async:
process.nextTick(function() {
acc.push(item * 2)
callback(null)
});
}, function(err, result) {
// result is now equal to [2, 4, 6]
});
async.transform({a: 1, b: 2, c: 3}, function (obj, val, key, callback) {
setImmediate(function () {
obj[key] = val * 2;
callback();
})
}, function (err, result) {
// result is equal to {a: 2, b: 4, c: 6}
})
- Source: