---
title: "Search as you type"
url: "https://www.mikefluff.com/en/experiments/js-ch13-typeahead"
locale: "en"
author: "Mikhail Savchenko (Mike Fluff)"
updated: "2026-11-18"
description: "The typeahead race condition and RxJS switchMap"
book: "https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems"
alternates:
  - locale: "en"
    url: "https://www.mikefluff.com/en/experiments/js-ch13-typeahead"
    markdown: "https://www.mikefluff.com/en/experiments/js-ch13-typeahead.md"
  - locale: "ru"
    url: "https://www.mikefluff.com/ru/experiments/js-ch13-typeahead"
    markdown: "https://www.mikefluff.com/ru/experiments/js-ch13-typeahead.md"
  - locale: "pt"
    url: "https://www.mikefluff.com/pt/experiments/js-ch13-typeahead"
    markdown: "https://www.mikefluff.com/pt/experiments/js-ch13-typeahead.md"
  - locale: "es"
    url: "https://www.mikefluff.com/es/experiments/js-ch13-typeahead"
    markdown: "https://www.mikefluff.com/es/experiments/js-ch13-typeahead.md"
---

# Search as you type

*The typeahead race condition and RxJS switchMap*

## What does switchMap do in RxJS?

switchMap is an RxJS operator that turns each value of a source stream into a new inner stream and keeps only the latest one. When a new value arrives, it unsubscribes from the previous inner stream before subscribing to the next. In RxJS unsubscribing is cancellation: a timer is cleared, a request made with RxJS ajax is aborted. That makes switchMap the usual answer for search as you type, where every keystroke starts a request and only the answer to the latest query should reach the screen.

A search box that asks the server on every keystroke sends its requests in order, and the answers come back in whatever order the network likes. If the code shows each answer as it arrives, the screen ends up with whichever came last, and that can be the answer to a query the user typed two letters ago.

Press “Type ‘meteor’”: six letters go in 50 ms apart, as in the book’s experiment, and each one sends a request to a fake server that answers after 20 to 300 ms. The latencies come from the book’s seeded generator, so a given seed always gives the same delays; change the seed to get another run, or type yourself. The timeline shows each request’s bar and when its answer lands. On the switchMap tab every new letter aborts the previous request with an AbortController, which is what switchMap does by unsubscribing, and the cancelled bars stop short. The “100 runs” button replays seeds 1 to 100 in both modes on real timers.

In the book, with RxJS 7.8.2 on Node 24, the promise version showed “meteor” in 55 runs out of 100. In the other 45 the screen kept a stale answer such as “mete” or “meteo”, and all six requests ran to the end. With switchMap the screen showed “meteor” every time, and three or four of the six requests were cancelled before they answered.

The chapter recalls that a promise cannot be cancelled, so cancellation has to be passed along by hand. A stream in RxJS cancels itself once nobody needs it. RxJS left the programmer the layout of the pipeline and took time, order and cancellation on itself.

_The page has an interactive experiment that runs in a browser: https://www.mikefluff.com/en/experiments/js-ch13-typeahead_

## Sources

- [RxJS: switchMap](https://rxjs.dev/api/operators/switchMap)
- [MDN: AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)

## From the book

Beside it in the book:

> The more the runtime decides, the cheaper it is to write code, and the harder it is to understand why something was recomputed or, conversely, was not. The dependency graph Tracker builds on the fly is written down nowhere, and understanding it requires separate tools. In the next chapter Meteor will stretch this graph across the network. Data lying in a database on the server will become reactive sources for functions running in the user’s browser, and the question «who depends on whom» will become a question of who has the right to see which data.

- Read the chapter: [Chapter 13. From MVC to the Reactive Graph](https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch13) (opening free, rest in the full book; Markdown: https://www.mikefluff.com/en/read/from-closures-to-autonomous-systems/ch13.md)

- Book: [JavaScript: From Closures to Autonomous Systems](https://www.mikefluff.com/en/books/from-closures-to-autonomous-systems)
