Skip to content

Instantly share code, notes, and snippets.

@banujan6
Created September 5, 2021 10:10
Show Gist options
  • Save banujan6/231c4b73e7dd79dbd2341c9bcb997524 to your computer and use it in GitHub Desktop.
Save banujan6/231c4b73e7dd79dbd2341c9bcb997524 to your computer and use it in GitHub Desktop.
medium-rxjs-scheduler
import { Observable, asyncScheduler } from 'rxjs';
import { observeOn } from 'rxjs/operators';
// "asyncScheduler" is a type of Scheduler which executes the Observable Asynchronously.
// "observeOn" is a Pipeable Operator that help us to re-emit the data with Scheduler.
const observable = new Observable((observer) => {
observer.next("First Data");
observer.next("Second Data");
observer.next("Third Data");
}).pipe(
observeOn(asyncScheduler)
);
console.log('just before subscribe');
// Since we are using "asyncScheduler", This code will be executed asynchronously.
observable.subscribe({
next(data) {
console.log(data)
}
});
console.log('just after subscribe');
// CONSOLE OUTPUT
// > just before subscribe
// > just after subscribe
// > First Data
// > Second Data
// > Third Data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment