Chaining promises using the then() and done() functions
As with the specifications, you can not only use then
and done
functions to achieve a single task, but also for making a chain out of it. In such a way, you can also create your own conditions within the code that will make your code more powerful, optimized, and logical. There are certain limitations though and these are also logical. You can add multiple then()
such as then().then().then()
, but you cannot do something like then().done().then()
. You may be wondering about the logic behind this. With every then()
, it returns a promise, which you can input to the next then()
function, but when you add done()
, it returns undefined
, which breaks the logic of a promise, yet you will get nothing ...
Get Mastering JavaScript Promises now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.