약속 체인이있는 경우 이미 약속이 모두 시작되었으므로 이미 시작되었으므로 약속을 모두 시작하거나 중지 할 수있는 방법이 없습니다 (취소 할 수는 있지만 가능한 한 멀리 할 수 있습니다).
var promises = /* where you get them, assuming array */;
// reduce the promise function into a single chain
var result = promises.reduce(function(accum, cur){
return accum.then(cur); // pass the next function as the `.then` handler,
// it will accept as its parameter the last one's return value
}, Q()); // use an empty resolved promise for an initial value
result.then(function(res){
// all of the promises fulfilled, res contains the aggregated return value
}).catch(function(err){
// one of the promises failed,
//this will be called with err being the _first_ error like you requested
});
그래서 덜 주석 버전 :
var res = promises.reduce(function(accum,cur){ return accum.then(cur); },Q());
res.then(function(res){
// handle success
}).catch(function(err){
// handle failure
});
감사
당신이
F1,... Fn
약속을 리턴하는 함수을 가정하면, 약속의 기본 빌딩 블록, 이에 대한 우리의.then
을 사용할 수 있습니다 ! 그러나 마지막 "그때"에 인수로 전달 된 "집계 된 결과"가 내 자신의 편의에 따라 구현해야하는 것입니까? – Guid@ Guid 그것은 당신의 API에 달려있다. 나는 각 약속이 이전 질문의 결과를 필요로한다고 생각했다. 예를 들어, 배열 밖에서 배열을 만들고 ('return accum.then (cur);') 루프 앞에서 루프에 'push'한 다음 결과를'.return '할 수 있습니다. –