Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript-promises-then-vs-then-catch/ #132

Open
utterances-bot opened this issue Jul 23, 2021 · 7 comments
Open

javascript-promises-then-vs-then-catch/ #132

utterances-bot opened this issue Jul 23, 2021 · 7 comments

Comments

@utterances-bot
Copy link

JavaScript Promises: then(f,f) vs then(f).catch(f)

What's the difference between promise.then(fn, fn) and promise.then(fn).catch(fn) when using JavaScript promises?

https://dmitripavlutin.com/javascript-promises-then-vs-then-catch/

Copy link

medikoo commented Jul 23, 2021

catch(onFailure) is nothing more than alias for then(null, onFailure)

So difference between then(onSuccess, onFailure) and then(onFailure).catch(onError) is same as difference between then(onSuccess, onFailure) and then(onSuccess),then(null, onFailure), and here just by looking at that we can see that second one logically doesn't feel right.

If we're constructing if/else structure (fork success and error logic for given operation), to avoid potential side effects it's best if it's always constructed as then(onSucces, onFailure).

Using catch(onError) makes sense if we either (1) do not envision success handling for given operation, or (2) we want to catch any error that's result of given async flow.

In (2) case, it's good to ask ourselves question, whether it's a valid approach, as it actually mirrors a general try/catch as we would do over sync code, and that's not really practiced.
If not envisioned error happens it naturally should surface, and for that there's no need to end our async flow with .catch(onError)

First promise tutorials (and I think many still do) advised to always use final catch() error handler, as early promise implementations didn't communicate on unhandled errors, but that's no longer the case (and if still some are dragging on that, that should rather be handled via dedicated event listener for unhandled rejections and not a final .catch clause)

@panzerdp
Copy link
Owner

@medikoo Very interesting, thanks for sharing your detailed explanation!

Copy link

@medikoo So you recommend not to use .catch clause??

Copy link

fkereki commented Jul 25, 2021

There's another difference: if the "success" function throws an exception, a separate .catch() will get it, but if you do .then(success,error) it won't be caught by the "error" function.

Repository owner deleted a comment from g8up Jul 26, 2021
Copy link

This became super apparent to me since I work with code that has a lot of old school callbacks as well as promises and often have had to translate between the two.

This is correct:

function foo(callback) {
  promiseFoo()
    .then(
      result => callback(null, result),
      err => callback(err)
    );
}

This is not correct:

function foo(callback) {
  promiseFoo()
    .then(result => callback(null, result)
    .catch(err => callback(err);
}

In the latter case your call back will be called twice if for some reason the callback itself throws an error.

Copy link

What will be the state of the promise in this case? Is it rejected or resolved?

It should be resolved right? Since we are calling the success callback.

Copy link

Thank you Dmitri for this clear and accessible explanation. Much obliged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants