Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promises without `.then`

Tags:

promise

Is there any downside to using a promise with only .catch section, but without .then at all?

I'm asking about the cases where the resolution result is not needed, only error handling.

Is this a good pattern to rely on .catch only and skip .then?

Or is it something that depends on which promise implementation it is?

like image 386
vitaly-t Avatar asked Mar 24 '16 23:03

vitaly-t


People also ask

Is promise called without then?

The promise code does not run until after the alert.

What is then in promise?

The then method returns a Promise which allows for method chaining. If the function passed as handler to then returns a Promise , an equivalent Promise will be exposed to the subsequent then in the method chain. The below snippet simulates asynchronous code with the setTimeout function. Promise.

How do you resolve a promise then?

Promise resolve() method: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.

What is the use of then () method in promise?

The then () method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise. Note: If one or both arguments are omitted or are provided non-functions, then then will be missing the handler (s), but will not generate any errors.

What happens when a promise is called without a handler?

If the Promise that then is called on adopts a state ( fulfillment or rejection) for which then has no handler, the returned promise adopts the final state of the original Promise on which then was called. A Function called if the Promise is fulfilled.

What is the difference between a promise and a promise object?

While a Promise object is "pending" (working), the result is undefined. When a Promise object is "fulfilled", the result is a value. When a Promise object is "rejected", the result is an error object. You cannot access the Promise properties state and result. You must use a Promise method to handle promises.

What is the behavior of a promise in JavaScript?

A promise has a handler method. Once a Promise is fulfilled or rejected, the respective handler function will be called asynchronously. The behavior of the handler function follows a specific set of rules as stated here. Let's go over them one by one.


1 Answers

Conceptually, there's nothing wrong with an operation that only has an error handler and has nothing else to do upon successful completion. If that's all it needs, then that's fine. For example, suppose you're updating a server with some new data from the client. If the data is successfully sent to the server, there's nothing else to do because the operation is complete, but if there's an error, then there is perhaps something else to do (retry, inform the user, correct the data based on the error code, etc...).

To comment on whether that's the right way to design your specific code, we'd have to see the actual code and understand what it is doing and then form an opinion on whether that is the best way to structure that specific code.

If I was designing a general purpose function, I'd certainly provide both completion (resolving the promise) and error (rejecting the promise) so the caller could hook into either one. But it is really up to the caller which events they want to know about and if only the error matters, then just having a .catch() is fine.

like image 155
jfriend00 Avatar answered Sep 20 '22 11:09

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!