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

Use promise then() instead of catch() with callbacks #1679

Merged
merged 1 commit into from
Sep 10, 2019
Merged

Use promise then() instead of catch() with callbacks #1679

merged 1 commit into from
Sep 10, 2019

Conversation

feross
Copy link
Member

@feross feross commented Sep 7, 2019

What is the purpose of this pull request? (put an "X" next to item)

[ ] Documentation update
[x] Bug fix
[ ] New feature
[ ] Other, please explain:

What changes did you make? (Give an overview)

Addresses my review here: #1419 (review)

Is there anything you'd like reviewers to focus on?

Copy link
Member

@Borewit Borewit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I think the bigger improvements for readability are

  • inline variables string=${x} instead of concatenations like "string=" + x
  • getting rid of redundant return
  • and function (x) to x =>

@feross
Copy link
Member Author

feross commented Sep 10, 2019

@Borewit These changes weren't about readability but about correctness. The .catch will catch more than just the promise rejections from the tasks array. It will also catch any exceptions thrown from the handler in .then() which means that if the cb function throws an exception, then we'll catch it here. Instead we should use .then(success, fail) for correct behavior.

@feross feross merged commit 4a10a2d into master Sep 10, 2019
@feross feross deleted the then branch September 10, 2019 18:05
@Borewit
Copy link
Member

Borewit commented Sep 10, 2019

I honestly don't see the difference Ferros.
I think in either cases it will catch all.

The catch() method returns a Promise and deals with rejected cases only. It behaves the same as calling Promise.prototype.then(undefined, onRejected)
source: developer.mozilla.org

The catch method on a promise is pretty simple because it is an alias for then(null, errorCallback). source: codingame.com

@feross
Copy link
Member Author

feross commented Sep 10, 2019

@Borewit There's a difference. See https://stackoverflow.com/a/33278420/292185 Let me know if it still doesn't make sense and I'll try to explain further.

@Borewit
Copy link
Member

Borewit commented Sep 10, 2019

Ah yes, you are right Feross! I completely missed the point.

catch(errorCallback)` is an alias

for

then(null, errorCallback)

where the alias is appended and not instead of the error handler of the previous then (should say the promise it is calling).

@codealchemist
Copy link
Contributor

codealchemist commented Sep 10, 2019

This has an interesting behavior:

const p1 = new Promise((resolve, reject) => {
  throw(new Error('Promise 1 THROWN'))
})

const p2 = new Promise((resolve, reject) => {
  setTimeout(() => {
    throw(new Error('Promise 2 THROWN'))
  })
})

p1
  .then(null, message => console.log('P1 failed:', message))
  .catch(message => console.log('P1 catched:', message))

p2
  .then(null, message => console.log('P2 failed:', message))
  .catch(message => console.log('P2 catched:', message))

You can run it here.
If the code fails synchronously, the failure callback of then will be called.
If there's no failure callback it will naturally flow thru catch.
If it fails asynchronously, it won't be caught by neither the failure callback nor the catch.

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

Successfully merging this pull request may close these issues.

3 participants