geilt

Promises are amazing.

Dec 5th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. Loan.destroy({ id: id })
  2. .then(function(result){
  3. return LoanInvestor.find({loan: id})
  4. .then(function(loanInvestors){
  5. return loanInvestors.map(function(investor){
  6. return investor.id;
  7. });
  8. });
  9. })
  10. .then( function(LoanInvestorIds){
  11. return [
  12. LoanSchedule.destroy({loan: id}),
  13. LoanInvestor.destroy({loan: id}),
  14. LoanInvestorSchedule.destroy({loanInvestor: LoanInvestorIds}),
  15. LoanNote.destroy({loan: id}),
  16. LoanDocument.destroy({loan: id}),
  17. LoanTransaction.destroy({loan: id})
  18. ];
  19. })
  20. .spread( function(schedule, investor, investorSchedule, note, docs, transactions) {
  21. return result;
  22. })
  23. .then( function(result){
  24. Loan.subscribe(req.socket, id, ['destroy']);
  25. Loan.publishDestroy(id);
  26. res.json('Deleted Loan');
  27. }).error( function(err){
  28. res.json(409, err);
  29. });
Advertisement
Add Comment
Please, Sign In to add comment