Exercise - Retry Operator
Exercise
In this exercise we'll be combining several of the previous exercises to reinforce our learning.
The goal is to allow the user to fetch a random user from our API. The user can continue to fetch random users until they have requested a total of 5 invalid users.
The request API URL for fetching a user is: https://reqres.in/api/users/[id]
where the [id]
is replaced with the user's unique identifier.
The API currently has valid user's whose id is 1
through 12
.
A user id of 13
or greater will result in a 404 error.
- Open exercise on codesandbox.
- After the
map()
operator that returns a random integer between 10 and 15, use themergeMap()
operator, which will receive theid
of the user to fetch. - Within the
mergeMap()
operator use theajax.getJSON()
method to make an HTTP GET request to the API for the specified user id. - Use the
map()
operator to map the response to itsdata
property. - Use the
catchError()
operator to catch an error, and update theoutput
textarea'svalue
with the error'smessage
property value. Then, rethrow the error using thethrowError()
operator. - Use the
retry()
operator to retry errors from the API and to prevent the Observable of the click events from completion via the error notification. - Use the
finalize()
operator to add thecursor-not-allowed
andopacity-50
classes to thebtn
element when the retry count has extinguished.
Hints
- Note the
next()
callback function in the Observer. You can use similar code in thecatchError()
callback function in order to update the textarea value and scroll the textarea to the bottom. - Use the
Element.classList
property on thebtn
element to add the necessary classes.