Wait until the promise and nested are then completed

I am returning a promise from such a function:

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; 

Say the above function is called save.

In the calling function, I want to wait until the entire chain is complete and then do something. My code looks like this:

 var savePromise = self.save(); savePromise.then(function() { console.log('aftersave'); }); 

As a result, the aftersave is sent to the console, and the promises chain still works.

How can I do something after completing the whole chain?

+7
source share
1 answer

Instead of pasting promises, put them together.

 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; 
+7
source

All Articles