Ignore Stale Responses
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
Show one of three users, picked with buttons labelled "User 1", "User 2" and "User 3"; user 1 is picked at first. The fetchUser prop takes a user number and returns a Promise of a user such as { name: "Ada Lovelace" }. Whenever the pick changes (and when the component mounts), call fetchUser with the new number and show "Loading..." (three full stops) in place of the user until the reply arrives, then show the name in a heading. Replies can arrive in any order: only the reply for the user picked now may ever be shown, so a slow reply for an earlier pick must be ignored. The starter's default fetchUser makes user 1 slow, so you can see the problem in the preview.
Your code must pass
- Loads user 1 at first
- Shows "Loading..." while another user loads
- An older reply arriving first doesn't replace Loading...
- The latest pick wins even when an older reply arrives last