2016-07-20 2 views
2

노드 jsync forEach 루프에서 작업하고 있습니다. 특정 조건이 발생할 때 비동기 루프를 종료하고 싶습니다. 나는 콜백 콜백을 시도했다. 하지만 작동하지 않는 것 같습니다. 어떻게 해결할 수 있을까요? 비동기 루프를 종료 할 수 있습니까?특정 조건이 발생할 때 async.forEach 루프를 종료하는 방법 + 노드

async.forEach(usertype.permissions, function (permission, cb) { 
     if (usertype.type === 'super_admin') { 
     console.log('super admin') 
     flag = 1; 
     // here i want to exit from the loop 
     //return cb(); 
     } 
     if (permission.description === 'Can this use?' && permission.default_value.indexOf('YES') > -1) { 
     flag = 1; 
     return cb(); 
     } 
     else { 
     if (permission.description === 'Ca?' && permission.default_value.indexOf('YES') > -1) { 
     flag = 1; 
     console.log('permission to advance') 
     return cb(); 
     } 
     else { 
     cb(); 
      } 
      } 
     }, function() { 
     // The code i want to execute after exit from the loop 
    }) 

답변

2

async의 다양한 기능을 것이다 "출구 초기"당신은 콜백에 값을 전달하면 일반적으로 Error :

async.forEach(usertype.permissions, function (permission, cb) { 
    if(someCondition) return cb(new Error('I want to exit here')); 
}, function(err) { 
    // Finished 
}); 

그러나, 당신의 permission 개체에 대한 게터는 비동기 않는 한, I 비동기 라이브러리를 사용하는 이유를 모르겠습니다.

관련 문제