React traps · Chapter 12 of 16
Responses out of order
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Effects
- 3 tasks
- React
- In the all labs plan
The first 2 chapters of React traps are free. This one comes with the all labs plan.
Why you need this
Every request succeeded, and every setVenue was a fine update. On a fast connection the answers come back in the order they were asked, so it looks right. It needs an older request that is slower, which is exactly what the new server does with short text.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.jsx: show the wrong search's shows on purpose
Write reproduce() in repro.jsx. Build a fake searchShows(text) that does not answer straight away: it returns a promise and keeps its resolve function under the text it was called with. Render ShowSearch with your fake, type ka in the Search shows box, then kaagaz. Answer the search for kaagaz first, with [{ id: 'S09', title: 'Kaagaz Ki Nao' }], then the search for ka, with Kaagaz Ki Nao and { id: 'S19', title: 'Mummy Ka Phone' }, each inside act. Resolve to the titles of the list's items, in order.
The check runs your reproduce() twice: beside the original ShowSearch.jsx it must resolve to ['Kaagaz Ki Nao', 'Mummy Ka Phone'] (ka's answer, which came last), and beside a fixed ShowSearch.jsx to ['Kaagaz Ki Nao']. So ka's answer must arrive after kaagaz's. No real waiting: you give the answers, and reproduce() must finish within 5 seconds.
- Task 2
ShowSearch.jsx: the list matches what was typed
Fix ShowSearch({ searchShows }) in ShowSearch.jsx so the page only shows what belongs to the text in the box now, whatever order the answers arrive in. The answer for the text in the box shows its titles, one li each, in the order given; an answer for older text changes nothing. An error for the text in the box shows its message; an error for older text changes nothing. While the search for the text in the box waits, the paragraph Searching... shows, also on a second search after a first, and it goes away when that search answers or fails.
Keep the Search shows label, the list and the messages as they are. The check uses its own searches, answers them out of order, and makes an old search fail after a newer one answered: a fix that ignores old answers but still shows old errors is as wrong as the bug.
- Task 3
ShowSearch.test.jsx: a test that would have caught it
Write a vitest test file, ShowSearch.test.jsx, that would have caught this bug. The check runs your file three times: beside the original ShowSearch.jsx, where at least one test must fail; beside the fixed ShowSearch.jsx, where every test must pass; and beside a wrong fix that ignores an old search's answer but still shows its error, where at least one test must fail.
So test both ways a late search can hurt, with a fake searchShows whose answers and failures you give: type twice, answer the second search first and the first one last, and expect the second search's titles; and type twice, answer the second, then make the first fail, and expect no error message on the page. No real waiting: each test must finish within 5 seconds.
What you will learn
- repro.jsx: show the wrong search's shows on purpose
- ShowSearch.jsx: the list matches what was typed
- ShowSearch.test.jsx: a test that would have caught it
More in Effects
Build "Responses out of order" yourself
Parda's web app runs on React, and Rukhsana, its senior frontend engineer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of React traps are free. This one comes with the all labs plan.