Promises and async/await
Asynchronous work, error handling, and the model behind data fetching.
Asking a server for a show takes time. fetch starts that work now and gives
you a promise for the reply later.
then and catch
A promise starts as pending. Later it is fulfilled with a value, or rejected
with an error. .then runs your function once the value arrives.
The promise itself is not the show. It prints as pending, because the reply arrives later. The last line runs before it.
response.json() is a second promise. That is why there are two .then
calls: first the HTTP reply, then the show data.
A chain and a missing show
Returning a value from a .then passes it into the next .then. A thrown
error skips every later .then and goes to .catch.
fetch only rejects when the network fails. A 404 is still a reply, so you
check response.ok yourself.
Show 44933 is Severance. The second .then received the value the first
one returned. The missing id is a 404, so never printed: never appeared.
async and await
await waits for a promise and gives you its value. It only works inside a
function marked async.
The first log is the title. The second forgot await, so it is still a
pending promise. An async function always returns a promise, so load() is
read with .then.
Errors with try/catch
await turns a rejection into a thrown error, so ordinary try/catch
handles it.
The line after the failed check never ran. The finally block runs either
way.
Sequential vs parallel
Two awaits in a row wait one after the other. If neither result needs the
other, that is wasted time.
The first line waits for Severance, then Andor. The second line asks for both
at once. Promise.all keeps the results in the order you listed the
promises.
Promise.all rejects as soon as any one promise rejects. When you want every
result either way, use Promise.allSettled.
Common mistakes
- Forgetting
await. You get a pending promise instead of the value. - Assuming
fetchfails on 404. It does not. Checkresponse.ok. - Awaiting one search at a time. If the searches do not depend on each
other, use
Promise.all.
Exercise
loadAll searches for three shows one after another. Make it search for all
three at once, keeping the same order.
The first line must still read:
titles: Severance, Andor, The Bear
The second line should take about as long as one search, not three. Hint:
Promise.all takes an array of promises.