The most important characteristic of promises is that it normalizes into predictable form how we handle the success or failure of any function call, especially if that call may be asynchronous in nature.
And in that normalization, it leaves our program in a position to control and trust how it will proceed, rather than handing that control (via a callback continuation) off to an untrustable third-party.
I like the idea of chaining the promises in my code so that it looks nice, pretty nice and easier to read. After several months following this pattern, I have a situation where I need to break the chain promise flow, like stopping the flow. See the below snippet code:
From the above code, I am expecting the code to stop chain and move on to the .catch handler, but is not. I learned the code will continue the flow as I return with no value at line 9.
So, here is the way to break the promise chains, instead of returning with no value, I throw an Error to stop the flow:
Oh, yeah I use Bluebird to working with Promise…
Another useful way to think of then vs done: Promise.done() is the equivalent of Array.forEach while Promise.then() is the equivalent for Array.map. You use the first when you don’t want to create a new promise, and the second when you do.