i need to write the final object formed after a lot of async requests to a JSON file.
Here is the layout of my file
var finalObj = {};
for(i=0;i<100;i++){
request.get("http://example.com/get/" +i,function(err,res,body){
var tObj = json.parse(body);
finalObj[tObj.name] = tObj.value;
})
}
after all the response object is recieved, i want to save the finalObj to a json file. How do i do this?
Note that it should be
JSON.parse(body)to parse the body correctly.The easiest thing to do would be to use a counter which gets incremented upon completion, and detect whether you’re handled all requests;
See the File System module for details on the file writing side of things; thankfully they’ve got a
writeFilefunction which eases the process of writing to a file.