ce75783a68edc8f7b02d916d91e3830e8f230c4e14f14176afc70042aa0d9c505b23d0e761c63d6a1d8ddf3417ce81c71e0254c5bc3e3831539a539ec37421 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ![Async Logo](https://raw.githubusercontent.com/caolan/async/master/logo/async-logo_readme.jpg)
  2. ![Github Actions CI status](https://github.com/caolan/async/actions/workflows/ci.yml/badge.svg)
  3. [![NPM version](https://img.shields.io/npm/v/async.svg)](https://www.npmjs.com/package/async)
  4. [![Coverage Status](https://coveralls.io/repos/caolan/async/badge.svg?branch=master)](https://coveralls.io/r/caolan/async?branch=master)
  5. [![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)
  6. [![jsDelivr Hits](https://data.jsdelivr.com/v1/package/npm/async/badge?style=rounded)](https://www.jsdelivr.com/package/npm/async)
  7. <!--
  8. |Linux|Windows|MacOS|
  9. |-|-|-|
  10. |[![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)| -->
  11. 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.
  12. A pure ESM version of Async is available as [`async-es`](https://www.npmjs.com/package/async-es).
  13. For Documentation, visit <https://caolan.github.io/async/>
  14. *For Async v1.5.x documentation, go [HERE](https://github.com/caolan/async/blob/v1.5.2/README.md)*
  15. ```javascript
  16. // for use with Node-style callbacks...
  17. var async = require("async");
  18. var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
  19. var configs = {};
  20. async.forEachOf(obj, (value, key, callback) => {
  21. fs.readFile(__dirname + value, "utf8", (err, data) => {
  22. if (err) return callback(err);
  23. try {
  24. configs[key] = JSON.parse(data);
  25. } catch (e) {
  26. return callback(e);
  27. }
  28. callback();
  29. });
  30. }, err => {
  31. if (err) console.error(err.message);
  32. // configs is now a map of JSON data
  33. doSomethingWith(configs);
  34. });
  35. ```
  36. ```javascript
  37. var async = require("async");
  38. // ...or ES2017 async functions
  39. async.mapLimit(urls, 5, async function(url) {
  40. const response = await fetch(url)
  41. return response.body
  42. }, (err, results) => {
  43. if (err) throw err
  44. // results is now an array of the response bodies
  45. console.log(results)
  46. })
  47. ```