Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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 在操作完成时执行函数_Javascript_Node.js - Fatal编程技术网

Javascript 在操作完成时执行函数

Javascript 在操作完成时执行函数,javascript,node.js,Javascript,Node.js,我正在使用节点JavaScript,并试图在过去的循环完成后运行一个新函数。在下面的代码中,//循环遍历数据中的对象,以对其进行处理用于表示基本for循环遍历数据,其中数据被附加到数组中 对于每个响应,z增加1。然而,我经常发现z有时在数据处理之前达到40。我不能将z放在循环中,因为每个页面中都有许多对象 有谁能建议一种方法,只增加z,并在循环完成后检查它是否等于40 var req = http.request(headers, function(response) { respons

我正在使用节点JavaScript,并试图在过去的循环完成后运行一个新函数。在下面的代码中,
//循环遍历数据中的对象,以对其进行处理
用于表示基本for循环遍历数据,其中数据被附加到数组中

对于每个响应,z增加1。然而,我经常发现z有时在数据处理之前达到40。我不能将z放在循环中,因为每个页面中都有许多对象


有谁能建议一种方法,只增加z,并在循环完成后检查它是否等于40

var req = http.request(headers, function(response) {
    response.on('error', function (e) {
        //
    })
    response.on('data', function (chunk) {
        //
    });
    response.on('end', function() {
        // loop through objects in data, to process it
        z++
        if (z == 40) {
            newFunction(values, totals)
        };
    });
})
req.on('error', function(e) {
    //
})
req.end();

我同意trincot的观点,我想请你更具体一点。 但根据我收集的信息,您可以尝试使用async.each()。


我希望这会有所帮助。

在每个响应的循环中,将处理的对象数与响应中的对象总数进行比较。仅在达到该数字时增加z

loop through responses
  loop through objects in single response
    if ( data.length === counter) {
      z ++;
    }
    counter ++;
  }
  if ( 40 === z ) {
    newFunction(values, totals);
  }
}

我建议使用流行的异步模块中的async.timeseries

链接:

示例代码:)


“有谁能建议一种只增加z的方法,并在循环完成后检查它是否等于40?”这是您已经做过的,假设您的循环就是您的注释所在的位置。如果你的循环触发了一些异步任务,那么这些任务的完成与循环的完成无关。你能提供一些更具体的细节吗。“数据已处理”:哪些数据?哪种代码?如果您的问题与正在处理的数据有关,则必须提供相关代码。@trincot从HTTP请求接收的数据得到处理,但这通常是在“If(z==40){}”条件返回true之后。我希望在将数据放入数组/处理后运行“if(z==40){}”条件。把它加到你的问题上。谢谢。Async是您的朋友:)
// Required NPM Modules
var async = require('async')

// Run Async Times Series Function
// Will execute 40 times then make the final callback
async.timesSeries(40, asyncOperation, asyncTimesCallback)


/**
 * This is the primary operation that will run 40 times
 * @param {Number} n - Sort of like the index so if its running 40 times and its the last time its running its gonna be 39 or 40 not sure you may have to test that
 * @param {Function} next - Callback function when operation is complete see async times docs or example in this code
 */
function asyncOperation (n, next){

    var req = http.request(headers, function(response) {
        response.on('error', function (e) {
            return next(e, null)
        })
        response.on('data', function (chunk) {
            //
        })
        response.on('end', function() {
            // loop through objects in data, to process it
            return next(null, 'some value?')
        })
    })
    req.on('error', function(e) {
        return next(e, null)
    })
    req.end()

}


/**
 * This is run after the async operation is complete
 */
function asyncTimesCallback (err, results) {

    // Error Checking?
    if(err) {
        throw new Error(err)
    }

    // function to run wen operation is complete
    newFunction(results['values'], results['totals'])

}


/**
 * New Function The Callback Function Calls
 */
function newFunction(values, totals){

    console.log('Values', values)
    console.log('Totals', totals)

}