简体   繁体   English

Nodejs循环使用回调函数

[英]Nodejs looping with callback function

I am new to nodejs callback function. 我是nodejs回调函数的新手。 I have to find phones list between two price ranges given, if there are no 15 phones in the obtained list , I have to change the price range until I got 15 phones. 我必须在两个价格范围之间找到电话列表,如果获得的列表中没有15个电话,我必须更改价格范围,直到我有15个电话。 Actually I wrote function for searching phones from database in another file, from that file I send callback to calling function. 实际上我编写了从另一个文件中的数据库中搜索手机的功能,从该文件中我将回调发送到调用函数。 After I got callback I have to check the array size if array size is less than 15 then change the price range and have to call the same function like Recursion or looping. 在我收到回调后,如果数组大小小于15,我必须检查数组大小,然后更改价格范围,并且必须调用相同的函数,如递归或循环。 I am unable to handle the callback function. 我无法处理回调函数。 Please help me to write the correct code. 请帮我写出正确的代码。

 while(true ){
            console.log("around came")
             Search.findBestMobile(context.end_price , context.start_price , function(data){
                 console.log("Best Mobile");
                 size = data.hits.hits.length;
                 if(size >= 15){
                     context.phone_list = makeStringFromArray(data.hits.hits);
                     cb(context);
                     break;
                 }else{
                     context.start_price += 1000;
                     context.end_price   += 1000;
                 }
             });
         }

But in above code, break is unreachable statement. 但是在上面的代码中,break是无法访问的语句。 I can't handle it. 我无法处理它。 please help me. 请帮我。

The problem is that you are trying to use asynchronous code in synchronous way. 问题是您正在尝试以同步方式使用异步代码。 That simply won't do, because callback can be called seconds later. 这根本不会做,因为可以在几秒钟之后调用回调。 Beside that break won't work if called inside function. 如果调用内部函数,那么中断将无效。 What are you really doing is calling Search.findBestMobile infinite number of times. 你真正在做什么是无限次地调用Search.findBestMobile。 Node.js has only single thread that it uses to run your code and you are blocking it. Node.js只有一个用于运行代码的线程,而你正在阻止它。 So callbacks won't be called. 所以不会调用回调。

What you need to do is to use recursion or other async approach to loops. 您需要做的是使用递归或其他异步方法来循环。 One simple way of doing it would be to call your outer function again with new context. 一种简单的方法是使用新的上下文再次调用外部函数。

function searchRange(context, cb) {
    Search.findBestMobile(context.end_price , context.start_price , function(data){
                 console.log("Best Mobile");
                 size = data.hits.hits.length;
                 if(size >= 15){
                     context.phone_list = makeStringFromArray(data.hits.hits);
                     cb(context);
                 }else{
                     context.start_price += 1000;
                     context.end_price   += 1000;
                     searchRange(context, cb);
                 }
             });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM