You can definitely make some mistakes.
Here are a bunch of examples of what happens in different scenarios to help get a sense for what happens when.
1. Normal errors
2. Errors inside Promises
3. Calling reject(…)
4. Not specifying a .catch(…)
5. Not specifying a .catch(…) when Promise resolves ok
Let’s now look at some examples with the es7
async await
syntax. Essentially, any function that is defined as async
automatically returns a promise where the .then(…)
contains its return value or if an error is thrown, then .catch(…)
is triggered with the error it experiences.
Furthermore, you can use
await
inside an async
function to automatically resolve the line as a promise. This will evaluate to the value inside the .then(…)
of that promise or throw an error with the value inside the .catch(…)
of that promise. Likewise, any errors bubble up in the same way that a reject
call would, which allows for the same handling of synchronous errors and promise rejections.6. Async functions and synchronous errors
7. Async functions and synchronous errors inside an await
8. Async functions and errors inside an await’s Promise
9. Async functions and calling reject(…) inside an await’s Promise
Left as a practice for the reader! (Spoiler alert: it’s the same result as the prior example.)
Some takeaways
- Always add a
.catch(…)
whenever you have a.then(…)
—you don’t want an unhandled Promise rejection! A common exception to this is if you’re returning a promise from a function and you expect whatever is calling that function to handle the catch. In that way, you can have one catch at the end of a bunch of promises. - If you expect a function to synchronously throw an error before a Promise, then you’ll want to wrap it in a
try-catch
, however, you likely don’t want functions to work like this and more likely such an error will be a coding bug that should be raised. (Excercise for the reader, explore the approach of starting your function withPromise.resolve(() => { … })
and putting the synchronous code in there.) - Inside
async
functions, synchronous errors and Promise rejections all bubble up as a Promise rejection in the function’s returned Promise object
Further exploration
- Try out chaining multiple
.then(…)
calls in a row. This avoids indenting ever deeper as you chain more calls. Both.then(…)
and.catch(…)
return a Promise, whether you return Promise or a regular variable from the from either. See how a Promise rejection will short-circuit a chain ofthen
calls to the nextcatch
. This also can simplify the your handling of Promise rejections to just one.catch(…)
with many Promises. - Chain
.then(…)
calls after a.catch(…)
and trigger different paths of execution. - See what happens when you throw an error inside a
.catch(…)
. You’ll get another unhandled Promise rejection! - Play around with how returning a value within a
.then(…)
keeps the Promise going and allows you to chain without embedding deeper and deeper. - Read some more about promises, e.g., “You’re missing the point of Promises”
- Check out these slides on Callbacks, Promises, and Async/Await that I presented in a talk at the Recurse Center.
Thank you for making it to the end! LMK if anything weird happens with the gifs, I just added the paused state and the play buttons using HTML5 canvas.
Không có nhận xét nào:
Đăng nhận xét