<commit_before>---
layout: post
title: Npm Async module - the last coke on the desert
category: web
---

This **awesome module** has saved me twice in the last days. Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for use with Node.js and installable via npm install async, it can also be used directly in the browser.

For more Information
<a class="fa fa-github" target="_blank" href="https://github.com/caolan/async/">Async Github</a>.

**Quick Example**

{% highlight js %}
var fs = require('fs');
var async = require('async');
var data = {};

// Iterate through all json files
//fs.readdir is asynchronous
fs.readdir('/jsonFiles', function (err, listFiles) {
    async.each(listFiles,
        function iterator(file, callback) {
            var filename = file.split('.')[0];
            data[namespace] = require(filename);
        },
        function (error) {
            /*
             * Here we know all files have been read
             * we can start using the data object
             */<commit_msg>update post: async module<commit_after>---
layout: post
title: Npm Async module - the last coke on the desert
category: web
---

This **awesome module** has saved me twice in the last days. Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for use with Node.js and installable via npm install async, it can also be used directly in the browser.

For more Information
<a class="fa fa-github" target="_blank" href="https://github.com/caolan/async/">Async Github</a>.

**Quick Example**

{% highlight js %}
var fs = require('fs');
var async = require('async');
var data = {};

// Iterate through all json files
//fs.readdir is asynchronous
fs.readdir('/jsonFiles', function (err, listFiles) {
    async.each(listFiles,
        function iterator(file, callback) {
            var filename = file.split('.')[0];
            data[namespace] = require(filename);

            /*
             * if you have completed your job
             * just invoke the callback to set it completed
             */

            callback(null);
        },
        function (error) {
            /*
             * Here we know all files have been read
             * we can start using the data object
             */
{'fraction_non_alphanumeric': 0.1038637307852098, 'fraction_numerical': 0.0008309098462816784, 'mean_word_length': 2.6932515337423313, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5611529', 'n_tokens_mistral': 619, 'n_tokens_neox': 600, 'n_words': 262}