-
Notifications
You must be signed in to change notification settings - Fork 0
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
Comments
So difference between 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 Using 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. First promise tutorials (and I think many still do) advised to always use final |
@medikoo Very interesting, thanks for sharing your detailed explanation! |
@medikoo So you recommend not to use |
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. |
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:
This is not correct:
In the latter case your call back will be called twice if for some reason the callback itself throws an error. |
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. |
Thank you Dmitri for this clear and accessible explanation. Much obliged! |
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/
The text was updated successfully, but these errors were encountered: