In my code, I have conditional tasks that all return a promise. I need tasks to be performed sequentially.
My current implementation looks something like this:
var chain = []; if () { chain.push(function(doContinue){ taskA().then(doContinue); }); } if () { chain.push(function(doContinue){ taskB().then(doContinue); }); } if () { chain.push(function(doContinue){ taskC().then(doContinue); }); } var processChain = function () { if (chain.length) { chain.shift()(processChain); } else { console.log("all tasks done"); } }; processChain();
This works great, but initially I was looking for a way to create a chain using only Promises and whole functions with .then , but I could not get a working solution.
If there is a cleaner way using only Promises and then call chains, then I would like to see an example.
source share