After having read dozens of articles on how great es6 promises are and why we should implement them I have been left with the feeling that ALL of my (non-trivial) javascript functions should be promises.
In fact I feel great when writing code using them since I avoid the triangle of doom and seemingly get clear and concise code. (it really makes reasoning about execution much simpler).
What I haven't been able to find is: When do you NOT use promises? When do I avoid using them?
Update:
While I have seen some great points like API consistency I have yet to find a solid NO case. The answer by Lux suggests that operations that fetch event emitters should avoid them since recurring callbacks are incompatible with promises. I do however feel like the answer is still lacking in substance to check it(as correct) right now.
Cons of using Promises over callbacks:It kills the purpose of asynchronous non-blocking I/O. Only one object can be returned. We cannot return multiple arguments.
Closures and Promise are different concepts. Closures refers to scope of variables where as promise are used to 'promise' that an act on something will occur when it is done on an asynchronous action.
They can handle multiple asynchronous operations easily and provide better error handling than callbacks and events. In other words also, we may say that, promises are the ideal choice for handling multiple callbacks at the same time, thus avoiding the undesired callback hell situation.
JavaScript | Promises. Promises are used to handle asynchronous operations in JavaScript. They are easy to manage when dealing with multiple asynchronous operations where callbacks can create callback hell leading to unmanageable code.
They are easy to manage when dealing with multiple asynchronous operations where callbacks can create callback hell leading to unmanageable code. Prior to promises events and callback functions were used but they had limited functionalities and created unmanageable code.
This second promise (promise2) represents the completion not just of doSomething (), but also of the successCallback or failureCallback you passed in, which can be other asynchronous functions returning a promise.
Perform operations inside the callback function and if everything went well then call resolve. If desired operations do not go well then call reject. promise. }). Promises can be consumed by registering functions using .then and .catch methods. then () is invoked when a promise is either resolved or rejected.
Some rules of thumb:
When your method can be synchronous (simple data transformation), then use synchronous code in that method.
However if the method can be synchronous sometimes, and asynchronous sometimes (multiple code paths based on internal or external state), it should be asynchronous always. Otherwise, you may encounter unexpected subtle discrepancies in how your code behaves in complex scenarios, so it's best to avoid mixing the two attitudes.
[edit] As noted in comment, when your method is synchronous for now, but you strongly believe it might need to do async work at one point in the future, you might want to use promises from the beginning to avoid costly refactoring.
In general, your API should be consistent, so it's best to either use promises everywhere, or callbacks everywhere. This will make it easier to reason about the code.
If you are writing ultra high performance code and/or need low memory footprint, you may consider not using promises but callbacks, or using a promise library with a focus on performance, like bluebird, instead of the native Promise implementation / general use case polyfill.
[edit] Anyway, new additions to the web platform, like global fetch function, return a Promise
, and it seems that in the upcoming future more and more browser built-ins will be operating on promises. So if you're willing to write modern code, you're not going to escape promises.
You use promises for one-shot asynchronous operations. Initiate the operation, carry out the operation, notify a caller of completion or results or error and then done for good.
The situations where you do not use promises are those that are different than described above:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With