Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Async.js waterfall (functions defined outside of the array)

Async.js waterfall (functions defined outside of the array)

Problem

I am using async.js for the first time and would like to seperate out the function array into seperate functions. I have:

working code

    async.waterfall([
        function(callback)
        {
            var querySuccess = function (tx, result)
            {
                callback(null, result.rows.item(0).EventImportTime || "");
            };

            var queryError = function (tx, e)
            {
                callback("Query Error")
            };

            database.open();
            database.query("SELECT EventImportTime FROM Contact WHERE Contact.Id = ?", [contactId], querySuccess, queryError);
        },
        function(lastImportTime, callback)
        {
            var url = "";
            url += 'MobileGetvents.aspx?';
            url += '&LastImportTime=';
            url +=  lastImportTime;
            url += '&Format=JSON';

            callback(null, url)
        },
    ],
    function(err, result)
    {
        if (err)
            console.log("Error Happened");
        else 
        {
            console.log(result);
            getJSON(result, callback, 

errorCallback)
            }
        })

I want

async.waterfall([
            getLastImportTime(callback) ,
            buildUrl(lastImportTime, callback),
        ],
        //callback
);

However when I run this code it always returns

Uncaught ReferenceError: lastImportTime is not defined

Problem courtesy of: CrimsonChin

Solution

I would assume that you want this instead:

var getLastImportTime = function(callback) { };
var buildUrl = function(lastImportTime, callback) { };

async.waterfall([
            getLastImportTime,
            buildUrl,
        ],
        //callback
);
Solution courtesy of: Vincent Briglia

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Async.js waterfall (functions defined outside of the array)

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×