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 theidof 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 itsdataproperty. - Use the
catchError()operator to catch an error, and update theoutputtextarea'svaluewith the error'smessageproperty 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-allowedandopacity-50classes to thebtnelement 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.classListproperty on thebtnelement to add the necessary classes.