How does RxJS create or simulate asynchronism?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
RxJS does not automatically make code asynchronous just because it uses observables. An observable can emit synchronously or asynchronously depending on its source and scheduler. What RxJS really provides is a uniform model for handling values over time, whether those values come from timers, events, Promises, or immediate in-memory data.
Observables Are Often Synchronous
A common misconception is that subscribe always means "later." It does not:
This prints:
beforevalue 1value 2value 3after
The emissions happen synchronously because of can produce the values immediately during subscription.
Where Asynchronism Actually Comes From
RxJS usually works with sources that are already asynchronous in the JavaScript runtime:
- DOM events
- timers
- Promises
- AJAX requests
- WebSocket messages
For example, interval uses the timer system:
This is asynchronous because the browser or Node.js event loop schedules each timer callback later.
A Promise-backed observable is similar:
RxJS is not inventing the async behavior there. It is adapting an already-async source into the observable model.
Schedulers Can Delay Delivery
RxJS can also make delivery asynchronous by scheduling notifications. One common tool is observeOn(asyncScheduler):
Now after prints before the values because RxJS uses the async scheduler to defer the notifications.
This is often what people mean by RxJS "simulating" asynchronism. The values already exist, but RxJS can choose to deliver them later.
RxJS Is About Coordination, Not Threads
RxJS does not create operating-system threads for your JavaScript code. In typical browser or Node.js environments, the main model is still an event loop. RxJS helps structure time-based and event-based logic inside that model.
That is why operators such as map, filter, and scan transform streams, while operators such as switchMap, mergeMap, and concatMap coordinate asynchronous inner operations:
Here, RxJS is coordinating user input timing, cancellation behavior, and network results. The network request itself is still handled by the underlying runtime.
Cold Observables And Subscription Timing
Many observables are cold, which means the producer starts when you subscribe:
Whether the producer behaves synchronously or asynchronously depends on what happens inside that function. RxJS gives you the contract between producer and subscriber, not a blanket promise about timing.
Common Pitfalls
The biggest mistake is assuming every observable is asynchronous. of, many custom observables, and several operators emit synchronously unless a scheduler or async source changes the timing.
Another common issue is confusing asynchronism with concurrency. RxJS coordinates event streams, but it does not automatically move CPU-heavy work off the main thread. If you need parallel computation, you need another runtime mechanism.
Developers also often reach for switchMap, mergeMap, and concatMap without understanding their different cancellation and ordering behavior. Those operators all handle async inner streams, but not in the same way.
Finally, schedulers change timing semantics. A stream that behaves fine synchronously may reveal race conditions or test failures once emissions are deferred.
Summary
- RxJS observables can be synchronous or asynchronous.
- Asynchronism usually comes from the underlying source, such as timers, Promises, events, or AJAX.
- RxJS can also defer notifications with schedulers such as
asyncScheduler. - The library is mainly about composing time-based flows, not creating threads.
- Do not assume an observable is async unless the source or scheduler makes it so.
Related reading
- How does the JavaScript heap handle recursion
- How does the JavaScript sort function workas an algorithm?
- How does the this keyword work, and when should it be used?
- How does thread pooling works, and how to implement it in an async/await env like NodeJS?
- How get Environment Variables from lambda nodejs aws-sdk
- How I can work with Amazon's Dynamodb Local in Node?
- How I can work with Amazon's Dynamodb Local in Node?
- How is an HTTP POST request made in node.js?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.