| 1 | 
|
|---|
| 2 |
|
|---|
| 3 | 
|
|---|
| 4 | [](https://www.npmjs.com/package/async)
|
|---|
| 5 | [](https://coveralls.io/r/caolan/async?branch=master)
|
|---|
| 6 | [](https://gitter.im/caolan/async?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|---|
| 7 | [](https://www.jsdelivr.com/package/npm/async)
|
|---|
| 8 |
|
|---|
| 9 | <!--
|
|---|
| 10 | |Linux|Windows|MacOS|
|
|---|
| 11 | |-|-|-|
|
|---|
| 12 | |[](https://dev.azure.com/caolanmcmahon/async/_build/latest?definitionId=1&branchName=master) | [](https://dev.azure.com/caolanmcmahon/async/_build/latest?definitionId=1&branchName=master) | [](https://dev.azure.com/caolanmcmahon/async/_build/latest?definitionId=1&branchName=master)| -->
|
|---|
| 13 |
|
|---|
| 14 | Async is a utility module which provides straight-forward, powerful functions for working with [asynchronous JavaScript](http://caolan.github.io/async/v3/global.html). Although originally designed for use with [Node.js](https://nodejs.org/) and installable via `npm i async`, it can also be used directly in the browser. An ESM/MJS version is included in the main `async` package that should automatically be used with compatible bundlers such as Webpack and Rollup.
|
|---|
| 15 |
|
|---|
| 16 | A pure ESM version of Async is available as [`async-es`](https://www.npmjs.com/package/async-es).
|
|---|
| 17 |
|
|---|
| 18 | For Documentation, visit <https://caolan.github.io/async/>
|
|---|
| 19 |
|
|---|
| 20 | *For Async v1.5.x documentation, go [HERE](https://github.com/caolan/async/blob/v1.5.2/README.md)*
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | ```javascript
|
|---|
| 24 | // for use with Node-style callbacks...
|
|---|
| 25 | var async = require("async");
|
|---|
| 26 |
|
|---|
| 27 | var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
|
|---|
| 28 | var configs = {};
|
|---|
| 29 |
|
|---|
| 30 | async.forEachOf(obj, (value, key, callback) => {
|
|---|
| 31 | fs.readFile(__dirname + value, "utf8", (err, data) => {
|
|---|
| 32 | if (err) return callback(err);
|
|---|
| 33 | try {
|
|---|
| 34 | configs[key] = JSON.parse(data);
|
|---|
| 35 | } catch (e) {
|
|---|
| 36 | return callback(e);
|
|---|
| 37 | }
|
|---|
| 38 | callback();
|
|---|
| 39 | });
|
|---|
| 40 | }, err => {
|
|---|
| 41 | if (err) console.error(err.message);
|
|---|
| 42 | // configs is now a map of JSON data
|
|---|
| 43 | doSomethingWith(configs);
|
|---|
| 44 | });
|
|---|
| 45 | ```
|
|---|
| 46 |
|
|---|
| 47 | ```javascript
|
|---|
| 48 | var async = require("async");
|
|---|
| 49 |
|
|---|
| 50 | // ...or ES2017 async functions
|
|---|
| 51 | async.mapLimit(urls, 5, async function(url) {
|
|---|
| 52 | const response = await fetch(url)
|
|---|
| 53 | return response.body
|
|---|
| 54 | }, (err, results) => {
|
|---|
| 55 | if (err) throw err
|
|---|
| 56 | // results is now an array of the response bodies
|
|---|
| 57 | console.log(results)
|
|---|
| 58 | })
|
|---|
| 59 | ```
|
|---|