每当从里面rest一下

你好,我必须从while循环中断开。 在while循环中我调用了一个asynchronous函数。 我必须检查,如果该asynchronous调用的输出中的某个字段是空的,那么我不得不中断,否则我将再次调用该asynchronous函数。 我试过这个:

var options = { headers : { 'Fk-Affiliate-Id':'xxxxxxx' , 'Fk-Affiliate-Token' : 'xxxxxxxxxxxxxxxx' } }; var state = ['approved','tentative','cancelled','disapproved']; state.forEach(element => { options.url = 'https://affiliate-api.flipkart.net/affiliate/report/orders/detail/json?startDate='+startDate+'&endDate='+endDate+'&status='+element+'&offset=0'; loop : while(true){ // This is the async call request.get(options, (err, res, body) => { var data = JSON.parse(body); console.log(data); // I have to check whether next is empty or not ? if(data.next === ''){ // I will perform some action on data here break loop; } else{ // I will perform some action on data here options.url = data.next; } }); } }); 

但是这显示错误没有句法断裂。 如何摆脱while循环?

似乎你不需要while循环。 你只是想停止当你达到所期望的结果之一的状态。 这意味着您需要等到asynchronous调用完成后才能进行另一个状态。 其中一个解决scheme是使调用同步(如果可能的话)。 另一个解决scheme是为每个状态处理创build单独的函数,并从asynchronous调用callback中调用它:

 var state = ['approved','tentative','cancelled','disapproved']; // starting with first state processState(0); function processState(stateIdx){ if(stateIdx >= state.length){ // we tried all states and no success. return; } // some code request.get(options, (err, res, body) => { // some code if(data.next !== ''){ // we have more records for this state - call it one more time. processState(stateIdx); } else { // done with this state, try next one. processState(stateIdx + 1); } }); }