Created
September 5, 2021 10:10
-
-
Save banujan6/231c4b73e7dd79dbd2341c9bcb997524 to your computer and use it in GitHub Desktop.
medium-rxjs-scheduler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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