Created
September 23, 2021 10:41
-
-
Save famzil/6a7f6f176285a30a0d655b286ac010f9 to your computer and use it in GitHub Desktop.
Execution time of multiple observers on one observable
This file contains 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
// Execution time with multiple observers on the same observable | |
const currentTime$ = new Observable((subscriber) => { | |
const currentTime = new Date().toLocaleTimeString(); | |
subscriber.next(currentTime); | |
subscriber.complete(); | |
}); | |
// Observer n°1 | |
currentTime$.subscribe(currentTime => console.log("Current time (Observer 1): ", currentTime)); | |
// Observer n°2 | |
setTimeout( () => { | |
currentTime$.subscribe(currentTime => console.log("Current time (Observer 2): ", currentTime)); | |
}, 1000); | |
// Observer n°3 | |
setTimeout( () => { | |
currentTime$.subscribe(currentTime => console.log("Current time (Observer 3): ", currentTime)); | |
}, 2000); | |
// results | |
Current time (Observer 1):12:40:04 PM | |
Current time (Observer 2):12:40:05 PM | |
Current time (Observer 3):12:40:06 PM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment