UNPKG

3.22 kBMarkdownView Raw
1![Async Logo](https://raw.githubusercontent.com/caolan/async/master/logo/async-logo_readme.jpg)
2
3![Github Actions CI status](https://github.com/caolan/async/actions/workflows/ci.yml/badge.svg)
4[![NPM version](https://img.shields.io/npm/v/async.svg)](https://www.npmjs.com/package/async)
5[![Coverage Status](https://coveralls.io/repos/caolan/async/badge.svg?branch=master)](https://coveralls.io/r/caolan/async?branch=master)
6[![Join the chat at https://gitter.im/caolan/async](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/caolan/async?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
7[![jsDelivr Hits](https://data.jsdelivr.com/v1/package/npm/async/badge?style=rounded)](https://www.jsdelivr.com/package/npm/async)
8
9<!--
10|Linux|Windows|MacOS|
11|-|-|-|
12|[![Linux Build Status](https://dev.azure.com/caolanmcmahon/async/_apis/build/status/caolan.async?branchName=master&jobName=Linux&configuration=Linux%20node_10_x)](https://dev.azure.com/caolanmcmahon/async/_build/latest?definitionId=1&branchName=master) | [![Windows Build Status](https://dev.azure.com/caolanmcmahon/async/_apis/build/status/caolan.async?branchName=master&jobName=Windows&configuration=Windows%20node_10_x)](https://dev.azure.com/caolanmcmahon/async/_build/latest?definitionId=1&branchName=master) | [![MacOS Build Status](https://dev.azure.com/caolanmcmahon/async/_apis/build/status/caolan.async?branchName=master&jobName=OSX&configuration=OSX%20node_10_x)](https://dev.azure.com/caolanmcmahon/async/_build/latest?definitionId=1&branchName=master)| -->
13
14Async 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. A ESM/MJS version is included in the main `async` package that should automatically be used with compatible bundlers such as Webpack and Rollup.
15
16A pure ESM version of Async is available as [`async-es`](https://www.npmjs.com/package/async-es).
17
18For 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...
25var async = require("async");
26
27var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
28var configs = {};
29
30async.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
48var async = require("async");
49
50// ...or ES2017 async functions
51async.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```