开发者

how to find the end of execution in event driven programming

开发者 https://www.devze.com 2023-03-30 22:23 出处:网络
Here is my code . db.query(str, arr, function selectCb(error, results, fields) { if (error) { return proceed(false, {errno:\'010\',message:error.message}, request);

Here is my code .

db.query(str, arr, function selectCb(error, results, fields) {
        if (error) {
            return proceed(false, {errno:'010',message:error.message}, request);
        }
        for (var i=0; i<results.length; i++) {
            // add the gib infor
            if (results[i].refertype=='G') {
                var input={};
                input.fields="*";
                input.gibname=results[i].refername;
                gib.getgibinternal(input, makeCallback(i));
                function makeCallback(index) {
                    return function(gresult) {
                        results[index].gib=gresult.data[0];
                        if (index==results.length-1) {
                            // becuase problem was comint in yapi enterothers  it give onlye one result of gib 
                            re开发者_开发百科turn proceed(true, results, request);
                        }
                    }
                }
                // add the user info        
            } else if(results[i].refertype=='U') {
                var input={};
                input.username=results[i].refername;
                input.fields="*";
                user.getuserinternal(input, makeCallbackuser(i));
                function makeCallbackuser(index) {
                    return function(gresult) {
                        results[index].user=gresult.data[0];
                        if(index==results.length-1) {
                            return proceed(true, results, request);
                        }
                    }
                }
            } 
        }
        if (results.length==0) {
            return proceed(true, results, request);
        }  

    });
});

The problem in this code is when i am having 5 records of all the refertype G and 5 record of refertype U then this function returned without wait for user data to come .

Suppose refeter type G is at last then and user data is coming late from server so when the last G arrived it return without the user data . What i want , is to execute all the query then return the value , how can i do this ?

how i make wait for user data to come ?


Libraries like async (https://github.com/caolan/async) have helpers for this sort of thing. Check out the docs as they show a few different scenario examples.

0

精彩评论

暂无评论...
验证码 换一张
取 消