あなたがコールバックを渡し、通知を扱うことができる:
function updateDataset(...) {
return a().then(function() {
console.log("[someService] Task 1 done...");
return b(...).then(function (entries) {
console.log("[someService] Task 2 done...");
var requests = c(entries);
return Promise.all(requests).then(function() {
console.log("[someService] Task 3 done...");
return true;
});
});
});
}
呼び出し先は次のようになりますさらに、すべての約束が返されるようにチェーンを「平坦化」することができ、.then
可能なアプローチを示すためにコードを少しリファクタリングしました。
function updateDataset(notificationCb) {
return a()
.then(function() {
console.log("[someService] Task 1 done...");
notificationCb(1); //first ended
return b(...);
})
.then(function (entries) {
console.log("[someService] Task 2 done...");
var requests = c(entries) ;
notificationCb(2); //second ended
return Promise.all(requests);
})
.then(function() {
console.log("[someService] Task 3 done...");
//here there's no need to call the callback because the fn returns to the caller
return true;
});
}
と、発信者:
someService.updateDataset(function(notification){
//here you choose the strategy to handle the notification
if(notification === 1){
//first has ended.. etc.
} else{
}
})
.then(function (isSucc) {
//Do stuff
});
を参照してください:http://stackoverflow.com/q/32909694/2446102 – Ginden