I am new to promises and although I am enjoying the simplicity of callbacks after dealing with callback hell but I am having some issues with trying to chain them together as you can see by the mess below. So basically what I am trying to achieve is to run through the chain and if there are no duplicate people found then hit the catch and create the person. If not then go down the chain through the subsequent promises and it works but i feel like i am going about this in the wrong manner.
self.getPotentialPersonDuplicates()
.then(function (result) {
return result.length > 0
? self.promptUserToPickFromDuplicatePersons(result)
: Promise.reject(new Error("There where no duplicates found"));
})
.then(function (result) { return result ? self.selectPersonFromDuplicatePersonLookup() : Promise.resolve(); })
.then(function (result) { return result ? self.addAsParticipant(redirect) : Promise.resolve(); })
.catch(function (e) {
console.log(e.stack);
return self.createOdsPerson()
.then(self.addAsParticipant.bind(self, redirect));
})
.then($ui.stopWaiting());
The method in there contain a lot of code and I don't think there are any code review related issues so I have not included them.