Home
Docs
GitHub
Pricing
Blog
Log In

Run Sandworm Audit for your App

Get started
Hold on, we're currently generating a fresh version of this report
Generated on Nov 28, 2023 via pnpm

async 2.0.0-rc.4

Higher-order functions and common patterns for asynchronous code
Package summary
Share
0
issues
0
licenses
Package created
19 Dec 2010
Version published
5 May 2016
Maintainers
5
Total deps
0
Direct deps
0
License
MIT

Issues

0
This package has no issues

Frequently Asked Questions

What does async do?

Async is a powerful utility module in JavaScript designed for working with asynchronous JavaScript. This tool provides straightforward functions that make it easier to manage and organize asynchronous code in JavaScript. It's particularly practical for use with Node.js but is also highly useful for coding directly in the browser. A version of Async, async-es, catered specifically for ESM is available as well.

How do you use async?

Async can be utilized using Node-style callbacks or ES2017 async functions. To use Async, you need to first install it with npm using the command npm i async. Once installed, you require it in your JavaScript file, as shown in the following examples:

var async = require("async");

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);
    doSomethingWith(configs);
});

In this snippet, the async.forEachOf function is used to iterate over each key-value pair in the obj object. It reads files and parses JSON data in an async way. If an error occurs, the callback function is called with the error. If no error occurs it does something with the configs.

Here is another example of how to use Async with ES2017 async functions:

var async = require("async");

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);
});

In this snippet, async.mapLimit function is used for applying the async function to each URL in the urls array. It does this with concurrency limit of 5, then logs out results or throws an error if one occurs.

Where are the async docs?

The official documentation for Async can be found at this URL: https://caolan.github.io/async/. This provides extensive detail on the many functions and features Async affords, as well as clear examples of how and when to use them.