Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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
Node.js Node async.map如何在所有任务完成后调用函数本身_Node.js_Asynchronous - Fatal编程技术网

Node.js Node async.map如何在所有任务完成后调用函数本身

Node.js Node async.map如何在所有任务完成后调用函数本身,node.js,asynchronous,Node.js,Asynchronous,我有以下代码: function get_status(){ try { /* GET - status */ async.map(['http://url1.com/', 'http://url2.com/', 'http://url3.com/'], function(value, callback) { /* GET - request status */ request.post({ url: value, form

我有以下代码:

function get_status(){
    try {
  /* GET - status */
async.map(['http://url1.com/', 'http://url2.com/', 'http://url3.com/'], function(value, callback) {
     /* GET - request status */
     request.post({
          url: value,
          form: { 'mycustomdata': ""+mycustomdata+"" },
          method: 'POST'}, function(err, res, body) {
               /* CHECK - response */
               if (!err && typeof body !== 'undefined' && res.statusCode == 200) {

                   console.log('get status success...')         
                   callback();
               } else {
                   callback('failed to get status');
               }
          })
    }, function(err, results) {
        if (err) { 
            console.log(err); return false;
        } else {
            console.log('finished...') }
    })
}
我需要在async.map处理中的所有三个url都完成时(一些url响应一些不响应)调用它输出“finished…”的位置,并从该输出再次调用该函数…以便在每次处理所有url时都运行该函数

所以简言之,我需要async.map处理所有URL以输出到控制台“finished…”并再次运行相同的函数

但我只是从控制台得到

 get status success... 
 failed to get status..
 get status sucess..
我从来没有接到过电话

 'finished'...

这样我就可以计算函数itsef了……你能告诉我该如何编写吗?

你只需要将
err
参数传递到最终回调。我相信,从我上次使用async.js开始,您需要传入
null
作为第一个参数,以继续下一个函数,这里是最后一个函数。否则将作为错误跳转到最终函数

 callback(null, results);

如果你想让它再次调用自己,那只是简单的递归。将async.map包装到一个函数中,当
console.log('finished…')
运行时,再次调用该函数。

好的,我给出了完整的示例,以便您可以看到问题所在,并尝试更改代码:

var async       = require('async'),
request         = require('request');

var myUrls = [ 'http://56.123.65.86:8080/api/server_status/',
  'http://88.96.42.122:8080/api/server_status/',
  'http://12.23.32.25:8080/api/server_status/',
  'http://251.214.44.58:8080/api/server_status/',
  'http://122.23.32.54:8080/api/server_status/' ]

async.map(myUrls, function(url, callback) {
    console.log('getting url...'+url)
  request(url, function(error, response, html) {
    // Some processing is happening here before the callback is invoked
    if (typeof response !== undefined) {
        console.log('response ok...'+url)
        return callback(null, html)
    } else {
        console.log('response failed...'+url)
        return callback(error, html);
    }
  });
}, function(err, results) {
   if (results) {
       console.log('all finished...')
   }
});
我明白了:

getting url...http://56.123.65.86:8080/api/server_status/
getting url...http://88.96.42.122:8080/api/server_status/
getting url...http://12.23.32.25:8080/api/server_status/
getting url...http://251.214.44.58:8080/api/server_status/
getting url...http://122.23.32.54:8080/api/server_status/
需要得到这个:

getting url...http://56.123.65.86:8080/api/server_status/
getting url...http://88.96.42.122:8080/api/server_status/
getting url...http://12.23.32.25:8080/api/server_status/
getting url...http://251.214.44.58:8080/api/server_status/
getting url...http://122.23.32.54:8080/api/server_status/
all finished...
我无法完成所有的工作…所以它会一直保持在获取url的过程中,并且会一直保持下去…所以我不知道为什么所有的工作都完成了。。。处理完所有项目后未触发…

i替换回调('failed to get status');带有回调(null,“获取状态失败”);但是当所有三个URL都被处理时,我无法得到“完成”。。。从控制台日志…也许我没有正确地理解你?