Rol*_*ndG 7 javascript promise jquery-deferred
我正在从这样的函数返回一个promise:
resultPromise = dgps.utils.save(opportunity, '/api/Opportunity/Save', opportunity.dirtyFlag).then(function () {
self.checklist.saveChecklist(opportunity).then(function () {
self.competitor.save(opportunity.selectedCompetitor()).then(function ... etc.
return resultPromise;
Run Code Online (Sandbox Code Playgroud)
假设上面的函数叫做save.
在我想做的调用函数中,等待整个链完成,然后做一些事情.我的代码看起来像这样:
var savePromise = self.save();
savePromise.then(function() {
console.log('aftersave');
});
Run Code Online (Sandbox Code Playgroud)
结果是,当承诺链仍在运行时,'aftersave'被发送到控制台.
整条链完成后我该怎么做?
而不是嵌套承诺,链接它们.
resultPromise = dgps.utils.save(opportunity, '/api/Opportunity/Save', opportunity.dirtyFlag).then(function () {
return self.checklist.saveChecklist(opportunity);
}).then(function () {
return self.competitor.save(opportunity.selectedCompetitor());
}).then(function () {
// etc
});
// return a promise which completes when the entire chain completes
return resultPromise;
Run Code Online (Sandbox Code Playgroud)