summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorAlexander Early <alex@npmjs.com>2017-11-06 18:08:33 -0800
committerAlexander Early <alex@npmjs.com>2017-11-06 18:08:33 -0800
commit11adc19bcdadb382e9dcf99e4784356546f4f41f (patch)
treeab8e5121d209c2f423b46f94996ef00156b15abb /README.md
parent777ca0feeff77d89efe7e1aa642cf5729d0d88f8 (diff)
downloadasync-11adc19bcdadb382e9dcf99e4784356546f4f41f.tar.gz
docs: add some quick examples to the README
Diffstat (limited to 'README.md')
-rw-r--r--README.md37
1 files changed, 36 insertions, 1 deletions
diff --git a/README.md b/README.md
index 4abb6d1..b48c7de 100644
--- a/README.md
+++ b/README.md
@@ -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)
+})
+```