Skip to content

Instantly share code, notes, and snippets.

@famzil
Created September 23, 2021 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save famzil/6a7f6f176285a30a0d655b286ac010f9 to your computer and use it in GitHub Desktop.
Save famzil/6a7f6f176285a30a0d655b286ac010f9 to your computer and use it in GitHub Desktop.
Execution time of multiple observers on one observable
// 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