Synchronous
Synchronous
At this point, you might be thinking that RxJS is a promise-like equivalent for asynchronous code execution in JavaScript. And, you'd be mostly correct.
RxJS, and specifically, the Observable primitive is most often used for asynchronous streams of events (and data).
However, RxJS is not strictly asynchronous.
Let's look at an example to understand how RxJS is synchronous.
import { Observable } from 'rxjs';
console.log('before observable');
const observable = new Observable((subscriber) => {
console.log('before next');
subscriber.next('next');
console.log('after next');
});
console.log('after observable');
observable.subscribe(console.log);
In what order would you expect the console logs?
Because this code is entirely synchronous we observe (ha) the following in the console:
- before observable
- before next
- next
- after next
- after observable