You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
2.5 KiB

4 years ago
  1. ![Async Logo](https://raw.githubusercontent.com/caolan/async/master/logo/async-logo_readme.jpg)
  2. [![Build Status via Travis CI](https://travis-ci.org/caolan/async.svg?branch=master)](https://travis-ci.org/caolan/async)
  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. [![libhive - Open source examples](https://www.libhive.com/providers/npm/packages/async/examples/badge.svg)](https://www.libhive.com/providers/npm/packages/async)
  7. [![jsDelivr Hits](https://data.jsdelivr.com/v1/package/npm/async/badge?style=rounded)](https://www.jsdelivr.com/package/npm/async)
  8. Async is a utility module which provides straight-forward, powerful functions for working with [asynchronous JavaScript](http://caolan.github.io/async/global.html). Although originally designed for use with [Node.js](https://nodejs.org/) and installable via `npm install --save async`, it can also be used directly in the browser.
  9. This version of the package is optimized for the Node.js environment. If you use Async with webpack, install [`async-es`](https://www.npmjs.com/package/async-es) instead.
  10. For Documentation, visit <https://caolan.github.io/async/>
  11. *For Async v1.5.x documentation, go [HERE](https://github.com/caolan/async/blob/v1.5.2/README.md)*
  12. ```javascript
  13. // for use with Node-style callbacks...
  14. var async = require("async");
  15. var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
  16. var configs = {};
  17. async.forEachOf(obj, (value, key, callback) => {
  18. fs.readFile(__dirname + value, "utf8", (err, data) => {
  19. if (err) return callback(err);
  20. try {
  21. configs[key] = JSON.parse(data);
  22. } catch (e) {
  23. return callback(e);
  24. }
  25. callback();
  26. });
  27. }, err => {
  28. if (err) console.error(err.message);
  29. // configs is now a map of JSON data
  30. doSomethingWith(configs);
  31. });
  32. ```
  33. ```javascript
  34. var async = require("async");
  35. // ...or ES2017 async functions
  36. async.mapLimit(urls, 5, async function(url) {
  37. const response = await fetch(url)
  38. return response.body
  39. }, (err, results) => {
  40. if (err) throw err
  41. // results is now an array of the response bodies
  42. console.log(results)
  43. })
  44. ```