Using Async waterfall in node.js

195 views
Skip to first unread message

Alex Brodov

unread,
Sep 6, 2014, 5:52:26 PM9/6/14
to nod...@googlegroups.com

I have 2 function that i'm running asynchronously, i'd like to write them using the waterfalll model, the thing is that i don't know how.. Here is my code :

var fs = require('fs');
function updateJson(ticker, value) {
    //var stocksJson = JSON.parse(fs.readFileSync("stocktest.json"));
    fs.readFile('stocktest.json', function(error, file) {
        var stocksJson =  JSON.parse(file);



        if (stocksJson[ticker]!=null) {
            console.log(ticker+" price : " + stocksJson[ticker].price);
            console.log("changing the value...")
            stocksJson[ticker].price =  value;
            console.log("Price after the change has been made -- " + stocksJson[ticker].price);
            console.log("printing the the Json.stringify")
            console.log(JSON.stringify(stocksJson, null, 4));
             fs.writeFile('stocktest.json',JSON.stringify(stocksJson, null, 4) , function(err) {  
                            if(!err) {
                                console.log("File successfully written");
                            }
                            if (err) {
                                console.error(err);
                            }

                       }); //end of writeFile
        }
        else {
            console.log(ticker + " doesn't exist on the json");
        }
    });
} // end of updaJson 

Any idea how can i write it using the waterfall so i'll be able to control this, Please write me some examples because i'm new to node.js

Ep Ga

unread,
Sep 9, 2014, 4:47:22 PM9/9/14
to nod...@googlegroups.com

this.... is more of a discussion... not really that type of mailing list... I would visit freenode irc for such help. They are more likely to help you with this issue.

Tom Boutell

unread,
Sep 10, 2014, 9:56:07 AM9/10/14
to nod...@googlegroups.com
Have you read this?

https://github.com/caolan/async#waterfall

There's a simple example. i don't really see what more you could ask for there.
Reply all
Reply to author
Forward
0 new messages