Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js |在数组上循环,进行post调用并在数组中累积结果_Javascript_Node.js_Callback_Coffeescript - Fatal编程技术网

Javascript Node.js |在数组上循环,进行post调用并在数组中累积结果

Javascript Node.js |在数组上循环,进行post调用并在数组中累积结果,javascript,node.js,callback,coffeescript,Javascript,Node.js,Callback,Coffeescript,我希望像这样在Node.js中调用Node.js(我正在为Node.js使用coffeescript) 我的获取数据方法看起来像 get_data: (url, json_data, callback) -> throw "JSON obj is required" unless _.isObject(json_data) post_callback = (error, response) -> if error callback(error)

我希望像这样在Node.js中调用Node.js(我正在为Node.js使用coffeescript)

我的获取数据方法看起来像

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
问题是我无法从request.post收集结果到“test”数组中
我知道我在for循环中做了一些错误的事情,但不确定仅仅是查看代码(而不是尝试),问题似乎是“何时得到响应”,而不是“是否得到响应”。在for循环运行之后,您所做的就是将一组请求排队。您需要对其进行设计,以便在第一个响应之前不会出现对第二个响应的请求,或者(更好)您需要一种方法来累积响应并知道所有响应何时返回(或超时),然后使用不同的回调将控制权返回到程序的主要部分


顺便说一句,这是我为ActionScript创建的多文件加载程序的代码。因为I/O在ActionScript中也是异步的,所以它实现了我上面描述的累积方法。它使用事件而不是回调,但它可能会为您提供一些有关如何为CoffeeScript实现此功能的想法

只要看看你的代码(而不是试用),问题似乎在于“何时得到响应”,而不是“是否得到响应”。在for循环运行之后,您所做的就是将一组请求排队。您需要对其进行设计,以便在第一个响应之前不会出现对第二个响应的请求,或者(更好)您需要一种方法来累积响应并知道所有响应何时返回(或超时),然后使用不同的回调将控制权返回到程序的主要部分


顺便说一句,这是我为ActionScript创建的多文件加载程序的代码。因为I/O在ActionScript中也是异步的,所以它实现了我上面描述的累积方法。它使用事件而不是回调,但它可能会为您提供一些有关如何为CoffeeScript实现此功能的想法

您似乎无法知道所有请求何时返回。你真的应该考虑使用,但以下是你能做到的:

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()

您似乎无法知道所有请求何时返回。你真的应该考虑使用,但以下是你能做到的:

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()

(err,data)->test
只返回
test
,它没有添加任何内容
test@muistooshort您期望什么?也许他是想写
test.push data
?不确定。对不起,我是说test.push(数据)等价的JavaScript是
get_data(url,li,function(err,data){return test.push(data);})-这就是您所期望的吗?
(错误,数据)->test
只返回
test
,它没有添加任何内容您所期望的
test@muistooshort可能他是想写
test.push data
?不确定。对不起,我是说test.push(数据)等价的JavaScript是
get_data(url,li,function(err,data){return test.push(data);})-这就是您所期望的吗?我使用steps.js从request.post获取响应,并将其累积到数组中。感谢aaron的指导,我使用steps.js从request.post获取响应,并将其累积到数组中。谢谢艾伦的指导