중첩 된 약속을 피하기 위해 다음을 어떻게 최적화 할 수 있습니까? 그것은 작동하지만 마치 중첩 약속을 계속할 것 같습니다.중첩 된 약속 최적화
코드가 먼저 서비스를 인증하고 반환하면 API를 비동기 적으로 호출하여 해당 항목을 가져 오는 함수에 해당 서비스를 제공합니다.
new Promise(function(resolve, reject) {
auth.authenticate(resolve);
}).then(function(service) {
console.log('service', service);
new Promise(function(resolve, reject) {
lineItems.getLineItems(service, resolve, reject);
}).then(function(items) {
console.log('returned line items');
console.log(items);
}).catch(function(err){
console.log('error!', err);
});
});
이 코드 :
auth.authenticate
및/또는lineItems.getLineItems
외부 경우, 표준 nodejs callbak/errback 스타일을 따라, 당신은 약속을 반환하는이 기능을 래핑 할 수 당신이 [return] (http://stackoverflow.com/questions/37081508/resolving-an-array-of-promises-from-within-a-parent-promise/37084467#37084467)을하지 않기 때문에 오류가 발생하기 쉽습니다. 두 번째 약속. – jib