diff options
author | Alexander Early <alex@npmjs.com> | 2017-11-06 18:08:33 -0800 |
---|---|---|
committer | Alexander Early <alex@npmjs.com> | 2017-11-06 18:08:33 -0800 |
commit | 11adc19bcdadb382e9dcf99e4784356546f4f41f (patch) | |
tree | ab8e5121d209c2f423b46f94996ef00156b15abb | |
parent | 777ca0feeff77d89efe7e1aa642cf5729d0d88f8 (diff) | |
download | async-11adc19bcdadb382e9dcf99e4784356546f4f41f.tar.gz |
docs: add some quick examples to the README
-rw-r--r-- | README.md | 37 |
1 files changed, 36 insertions, 1 deletions
@@ -8,8 +8,43 @@ [![jsDelivr Hits](https://data.jsdelivr.com/v1/package/npm/async/badge?style=rounded)](https://www.jsdelivr.com/package/npm/async) -Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. 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. +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. For Documentation, visit <https://caolan.github.io/async/> *For Async v1.5.x documentation, go [HERE](https://github.com/caolan/async/blob/v1.5.2/README.md)* + + +```javascript +// for use with Node-style callbacks... +var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"}; +var configs = {}; + +async.forEachOf(obj, (value, key, callback) => { + fs.readFile(__dirname + value, "utf8", (err, data) => { + if (err) return callback(err); + try { + configs[key] = JSON.parse(data); + } catch (e) { + return callback(e); + } + callback(); + }); +}, err => { + if (err) console.error(err.message); + // configs is now a map of JSON data + doSomethingWith(configs); +}); +``` + +```javascript +// ...or ES2017 async functions +async.mapLimit(urls, 5, async function(url) { + const response = await fetch(url) + return response.body +}, (err, results) => { + if (err) throw err + // results is now an array of the response bodies + console.log(results) +}) +``` |