I wish to make a call in Node.js somethine like this (i m using coffeescript for node.js)
test = [] //initially an empty array
list = []//an array with 10 json object
for li in list
get_data url , li, (err,data) -> test.push data
my get_data method look like
get_data: (url, json_data, callback) ->
throw "JSON obj is required" unless _.isObject(json_data)
post_callback = (error, response) ->
if error
callback(error)
else
callback(undefined, response)
return
request.post {url: url, json: json_data}, post_callback
return
problem is I am not able to collect the result from request.post into the 'test' array I Know I am doing something wrong in the for loop but not sure what
You don't appear to have any way of knowing when all of the requests have returned. You should really consider using a good async library, but here's how you can do it:
test = [] //initially an empty array
list = []//an array with 10 json object
on_complete = ->
//here, test should be full
console.log test
return
remaining = list.length
for li in list
get_data url , li, (err,data) ->
remaining--
test.push data
if remaining == 0
on_complete()
In just looking at your code (not trying it out), the problem seems to be a matter of "when you'll get the response" rather than a matter of "if you'll get the response". After your for loop runs, all you have done is queue a bunch of requests. You need to either design it so the request for the second one doesn't occur until the first has responded OR (better) you need a way to accumulate the responses and know when all of the responses have come back (or timed out) and then use a different callback to return control to the main part of your program.
BTW, here is code for a multi-file loader that I created for ActionScript. Since I/O is asynchronous in ActionScript also, it implements the accumulating approach I describe above. It uses events rather than callbacks but it might give you some ideas on how to implement this for CoffeeScript.