-
-
Save jgololicic/7e5bd0ddea704621fbeff83ff3e84304 to your computer and use it in GitHub Desktop.
Promise vs 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
Single value (Promise) vs multiple values (observable) | |
------------------------------------------------------- | |
const numberPromise = new Promise((resolve) => { | |
resolve(5); | |
resolve(10); | |
}); | |
numberPromise.then(value => console.log(value)); | |
// still prints only 5 | |
const numberObservable = new Observable((observer) => { | |
observer.next(5); | |
observer.next(10); | |
}); | |
numberObservable.subscribe(value => console.log(value)); | |
// prints 5 and 10 | |
/** | |
let i = 0; | |
setInterval(() => { | |
observer.next(i++); | |
}, 1000); | |
*/ | |
+ similar syntax | |
Single response is good for Simple HTTP request, but multiple responses/subscriptions are good for: | |
+ setInterval calls, | |
+ webSockets, | |
+ DOM events (mouse clicks etc.), | |
------------------------------------------------------- | |
http://rxmarbles.com/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment