Last active
August 29, 2015 14:06
-
-
Save steve-ross/4ca5c8f99b5bc9ed1163 to your computer and use it in GitHub Desktop.
ticker with queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var async = require('async'); | |
function updateJson(ticker, value, cb) { | |
async.waterfall([ | |
function(next){ | |
fs.readFile('stocktest.json', next); | |
}, | |
function(fileContents, next){ | |
var stocksJson = JSON.parse(fileContents); | |
if (stocksJson[ticker]) { | |
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)); | |
var stocks_json_string = JSON.stringify(stocksJson, null, 4); | |
fs.writeFile('stocktest.json',stocks_json_string, next); | |
}else{ | |
next('nothing to write ' + ticker + ' was null'); | |
} | |
} | |
], cb); | |
} | |
var q = async.queue(function (stockdata, callback) { | |
updateJson(stockdata.ticker, stockdata.price, function(err, result){ | |
if(err){ | |
console.log(err); | |
} | |
// success | |
callback(); | |
}); | |
}, 1); | |
// assign a callback | |
q.drain = function() { | |
console.log('all items have been processed'); | |
} | |
q.push([{ticker: 'AAPL', price: '1000.33'},{ticker: 'BLAH', price: '111.22'}], function (err) { | |
console.log('finished processing ticker item'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment