Node.js的:在移动之前在循环中,等待回调 [英] Node.js: waiting for callbacks in a loop before moving on

查看:129
本文介绍了Node.js的:在移动之前在循环中,等待回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了里面的异步调用,具有回调的循环。为了能够继续前进,我需要回调一路过关斩将火整个循环,以随后显示循环的结果。

每次我试图控制这不起作用方式(曾尝试步骤,Tame.js,async.js,和其他人) - 关于如何推进任何建议

 阵列= ['中','文件''测试'];
数组2 = ['另一个','阵'];为(i的数组){
    项目=阵列[我]
    document_ids =新的Array();    对(在数组2 I2){
        ITEM2 =数组2 [12];
        //看看它
        mongodb.find({ITEM_NAME:ITEM2})的toArray(函数(ERR,文件{
            //因为异步的,
            //将code对移动以后调用此回
            的console.log('了ID');
            document_ids.push(DOCUMENT_ID);
        }))
    }    //使用document_ids
    的console.log(document_ids); //显示[]
    的console.log('完成');
}//显示:
// []
//完成
//得到ID
//得到ID


解决方案

您的回调炉火前你登录document_ids。你要跟踪你跑了多少回调知道什么时候就大功告成了。

这是简单的方法是使用一个计数器,并检查每个回调数。

以你的例子

  VAR阵列= ['测试','中','文件'];
VAR数组2 = ['另一个','阵'];
变种document_ids = [];变种等待= 0;为(i的数组){
    项目=阵列[我]    对(在数组2 I2){
        ITEM2 =数组2 [12];
        等待++;        mongodb.find({ITEM_NAME:ITEM2})的toArray(
            功能(ERR,DOCUMENT_ID){
                等待 - ;
                document_ids.push(DOCUMENT_ID);
                完成();
            })
        );
    }
}功能齐全(){
    如果(!等待){
        的console.log(document_ids);
        的console.log('完成');
    }
}

I have a loop that has an asynchronous call inside it, with a callback. To be able to move on, I need the callback to fire for the entire loop all the way through, to then display the results of the loop.

Every way I've tried to control this doesn't work (have tried Step, Tame.js, async.js, and others) - any suggestions on how to move forward?

array = ['test', 'of', 'file'];
array2 = ['another', 'array'];

for(i in array) {
    item = array[i];
    document_ids = new Array();

    for (i2 in array2) {
        item2 = array2[i2];
        // look it up
        mongodb.find({item_name: item2}).toArray(function(err, documents {
            // because of async,
            // the code moves on and calls this back later
            console.log('got id');
            document_ids.push(document_id);
        }))
    }

    // use document_ids
    console.log(document_ids); // shows []
    console.log('done');
}

// shows:
// []
// done
// got id
// got id

解决方案

You're logging document_ids before your callbacks fire. You have to keep track of how many callbacks you've run to know when you're done.

An easy method is to use a counter, and to check the count on each callback.

Taking your example

var array = ['test', 'of', 'file'];
var array2 = ['another', 'array'];
var document_ids = [];

var waiting = 0;

for(i in array) {
    item = array[i];

    for (i2 in array2) {
        item2 = array2[i2];
        waiting ++;

        mongodb.find({item_name: item2}).toArray(
            function(err, document_id) {
                waiting --;
                document_ids.push(document_id);
                complete();
            })
        );
    }
}

function complete() {
    if (!waiting) {
        console.log(document_ids);
        console.log('done');    
    }
}

这篇关于Node.js的:在移动之前在循环中,等待回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆