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: