6 ms·
I never wrapped my mind around the Promises API, but I have always async/await is much easier to grasp in any language
by fjp 7y ago
I never wrapped my mind around the Promises API, but I have always async/await is much easier to grasp in any language
- pintxo 7y agoMaybe because it should have looked like this (personal opinion): waitForPromise() .then(handleResolve) .fail(handleReject) .catch(handleException); But actually looks like this: waitForPromise() .then(handleResolve, handleReject) .catch(handleException);
- csnover 7y agowaitForPromise().catch(foo) is exactly the same thing as waitForPromise().then(undefined, foo). Your first example is essentially the same as: waitForPromise() .then(handleResolve) .catch(handleReject) .catch(handleException); With the exception that if `handleResolve` also throws then that will be picked up by `handleReject`, whereas in `waitForPromise().then(handleResolve, handleReject).catch(handleException)` it won’t.
- pintxo 7y agoAnd you last sentence is exactly describing the issue I have. This merging of exception handling and rejection handling into one function call feels like a bad idea. As initially I thought catch() is only handling promise rejections. Then suddenly I ended up in the catch() although the Promise resolved fine, just to learn that my handleResolve code threw an exception. Unsurprisingly my handleReject code was not prepared for this.