在node.js中的循环中调用回调函数

时间:2023-01-05 23:59:19

With the help of glortho in this thread i built this code:

在这个线程的glortho的帮助下,我构建了这个代码:

for(var i=0;i<datos.length;i++){
   bittrex.getticker(datos[i].Currency, function(err, data){
        if (err){
            console.log('ERROR:', err);
            return 'ERROR:'+ err;
        } else {
            if (data.message!='INVALID_MARKET') {
                 this.LasValueBTC=data.result.Last;
            } else {
                 this.LasValueBTC='';   
            }  
        }
    }.bind(datos[i]));
}

The problem is that outside the callback function the datos array is not updated...As it is written at the moment if i console.log(this) inside the function works great and this.LastValueBTC exists in my json, but outside the function if i console.log(datos) after the loop, the LastValueBTC does not exist..and i need to do a res.send(datos) after the loop..

问题是在回调函数之外,datos数组没有更新…就像现在写的,如果我。log(这个)在函数里面工作得很好,这个。LastValueBTC存在于我的json中,但是在函数之外,如果我在循环之后console.log(datos),那么LastValueBTC就不存在。我需要在循环之后做一个res.send(datos)。

2 个解决方案

#1


2  

What you need to do is wait for all the callbacks to complete and then call res.send.

您需要做的是等待所有回调完成,然后调用res.send。

var count = datos.length;
for(var i=0;i<datos.length;i++){
   bittrex.getticker(datos[i].Currency, function(err, data){
        if (err){
            console.log('ERROR:', err);
            return 'ERROR:'+ err;
        } else {
            if (data.message!='INVALID_MARKET') {
                 this.LasValueBTC=data.result.Last;
            } else {
                 this.LasValueBTC='';   
            }  
            count--;
            if (count === 0) {
                res.send(datos);
            }
        }
    }.bind(datos[i]));
}

Or using async

或者使用异步

async.each(datos, function(dato, next) {
     bittrex.getticker(dato.Currency, function(err, data) {
        if (err){
            console.log('ERROR:', err);
            next(err);
        } else {
            if (data.message!='INVALID_MARKET') {
                 dato.LasValueBTC = data.result.Last;
            } else {
                 dato.LasValueBTC='';   
            }  
            next();
        }
    });
}, function(err) {
   res.send(datos);
});

#2


1  

Go through this article http://www.richardrodger.com/2011/04/21/node-js-how-to-write-a-for-loop-with-callbacks/#.VTXnFa2eDGc

请阅读本文http://www.richardrodger.com/2011/04/21/node-js-how-to-write-a-for-loop- callbacks/#.VTXnFa2eDGc

It gives a good conceptual overview on what happens if you put functions inside for loop

它提供了一个很好的概念概述,说明如果将函数放在for循环中会发生什么

#1


2  

What you need to do is wait for all the callbacks to complete and then call res.send.

您需要做的是等待所有回调完成,然后调用res.send。

var count = datos.length;
for(var i=0;i<datos.length;i++){
   bittrex.getticker(datos[i].Currency, function(err, data){
        if (err){
            console.log('ERROR:', err);
            return 'ERROR:'+ err;
        } else {
            if (data.message!='INVALID_MARKET') {
                 this.LasValueBTC=data.result.Last;
            } else {
                 this.LasValueBTC='';   
            }  
            count--;
            if (count === 0) {
                res.send(datos);
            }
        }
    }.bind(datos[i]));
}

Or using async

或者使用异步

async.each(datos, function(dato, next) {
     bittrex.getticker(dato.Currency, function(err, data) {
        if (err){
            console.log('ERROR:', err);
            next(err);
        } else {
            if (data.message!='INVALID_MARKET') {
                 dato.LasValueBTC = data.result.Last;
            } else {
                 dato.LasValueBTC='';   
            }  
            next();
        }
    });
}, function(err) {
   res.send(datos);
});

#2


1  

Go through this article http://www.richardrodger.com/2011/04/21/node-js-how-to-write-a-for-loop-with-callbacks/#.VTXnFa2eDGc

请阅读本文http://www.richardrodger.com/2011/04/21/node-js-how-to-write-a-for-loop- callbacks/#.VTXnFa2eDGc

It gives a good conceptual overview on what happens if you put functions inside for loop

它提供了一个很好的概念概述,说明如果将函数放在for循环中会发生什么