Sample Code - Async in Node.js

Posted in Node.js, Express.js, on 7/6/2014

A shot sample code to describe how to use npm async module waterfall process.

Since node.js, mongoose is async, if want to process your logic like, update step 1 do setup 2 and more. Use async module is one way.

 

Simple example of using async

Example code to use async waterfall
 async.waterfall(
    [
      function (callback) {
          console.info("1");
          callback(null, 'one', 'two');
      },
    function (arg1, arg2, callback) {
        console.info("2");
        console.info(arg1 + arg2);
        // arg1 now equals 'one' and arg2 now equals 'two'
        callback(null, 'three');
    },
    function (arg1, callback) {
        console.info("3");
        console.info(arg1);
        // arg1 now equals 'three'
        callback(null, 'done');
    }
    ], function (err, result) {
        console.info("4");
        console.info(err);
        console.info(result);
    });
The result value showing by above logic.
1
2
onetwo
3
three
4
null
done

Another example of using async with mongoose, nodejs

First I had a logic code written in C# with C# mongo driver. Following is the sample code.


 private object GetTagsItems()
        {
            var tags = DomainContext.DEALS.All().DistinctBy(x => x.TagId).Take(20);

            List tagItems = new List();

            foreach (var tag in tags)
            {
                List tagDeal = new List();
                var deals = DomainContext.DEALS.All(x => x.TagIds.Contains(tag.Id)).Take(4);
                foreach (var deal in deals)
                {
                    tagDeal.Add(new
                    {
                        Title = deal.Title,
                        SlugTitle = deal.SlugTitle,
                        ImageUrl = deal.PrimaryImageUrl,
                    });
                }
                tagItems.Add(new
                {
                    TagName = tag.Name,
                    TagId = tag.TagId,
                    Items = tagDeal
                });
            }

            return tagItems;
        }

I was trying to move the code to node.js with mongoose.First, I wrote something following in node.js. It didn't work. At the UI, data is always null. Then I noticed, js is none blocking, asyc not like C#. So i was trying to use node.js asyc module.


 var tagQ = tagLabel.find({}).limit(20);
    tagQ.exec(function (err, tags) {
        var result = {};
        var tagItems = [];
        tags.forEach(function (tag) {
            var dealQ = dealLineItem.find({}).where('DealId'). in ([1]).limit(4);
            dealQ.exec(function (err, dealItems) {
                if (!err) {
                    console.info(err);
                }

                var tagDeals = [];
                dealItems.forEach(function (dealItem) {
                    var deal = {};
                    deal.Title = dealItem.Title;
                    deal.SlugTitle = dealItem.SlugTitle;
                    deal.ImageUrl = dealItem.PrimaryImageUrl;
                    tagDeals.push(deal);
                })

                var tagItem = [];
                tagItem.TagName = tag.Name;
                tagItem.TagId = tag.TagId;
                tagItem.Items = tagDeals;

                tagItems.push(tagItem);
                result.name = 'test';
                result.items = tagItems;
                locals.data = result;
                return res.json(result);
            });
        });
        res.render('index.jade', { data: locals.data });
    });

Following is another example which works. Following example so after everything finish then code will go to async.parallel to do the task.


var items = [];
    var myCalls = [];
    tagIds.forEach(function (tagId) {
        myCalls.push(function (callback) {
            tagLabel.findOne({ TagId: tagId }, function (err, tag) {
                var dealQ = dealLineItem.find({ TagIds: mongoose.Types.ObjectId(tag._id) }).limit(4);
                dealQ.exec(function (err, dealItems) {
                    var deals = [];
                    dealItems.forEach(function (dealItem) {
                        var deal = {};
                        deal.Title = dealItem.Title;
                        deals.push(deal);
                    });
                    var tagItem = {};
                    tagItem.TagName = tag.Name;
                    items.push(tagItem);
                    callback(null, null);
                });
            });
        });

        async.parallel(myCalls, function (err, result) {
            if (err)
                return console.log(err);
            locals.data =
            {
                Tags: items
            };
            res.render('index.jade', { data: locals.data });
        });
    });