Last active
May 22, 2016 13:38
-
-
Save mattpodwysocki/ebaef80369029c59342a to your computer and use it in GitHub Desktop.
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
// If the ES6 Promise is available, it uses that, else: | |
Rx.config.Promise = RSVP.Promise; | |
// To promise with success | |
Rx.Observable | |
.just(42) | |
.toPromise(/* Optional Promise ctor like RSVP.Promise */) | |
.then( | |
result => console.log('Result', result), | |
reason => console.log('Reason', reason)); | |
// => Result 42 | |
// To promise with failure | |
Rx.Observable | |
.throw(new Error('woops')) | |
.toPromise(/* Optional Promise ctor like RSVP.Promise */) | |
.then( | |
result => console.log('Result', result), | |
reason => console.log('Reason', reason)); | |
// => Error woops | |
// Returning from another thenable | |
Promise | |
.resolve(42) | |
.then(result => Rx.Observable.just(result + result).toPromise() ) | |
.then(result => console.log('Result', result)); | |
// => Result 84 | |
// Composing with Promises | |
Rx.Observable | |
.just(42) | |
.flatMap((x, i) => Promise.resolve(x + x + i)) | |
.subscribe(result => console.log('Result', result)); | |
// => Result 84 | |
// Zip | |
Rx.Observable | |
.zip( | |
Promise.resolve(42), | |
Promise.resolve(56), | |
Promise.resolve(72), | |
(f,s,t) => f + s + t | |
) | |
.subscribe(result => console.log('Result', result)); | |
// => Result 170 | |
// Concat | |
Rx.Observable | |
.concat( | |
Promise.resolve(42), | |
Promise.resolve(56), | |
Promise.resolve(72) | |
) | |
.subscribe(result => console.log('Result', result)); | |
// => Result 42 | |
// => Result 56 | |
// => Result 72 | |
// OnErrorResumeNext | |
Rx.Observable | |
.onErrorResumeNext( | |
Promise.resolve(42), | |
Promise.reject(new Error('woops'), | |
Promise.resolve(56), | |
Promise.resolve(72) | |
) | |
.subscribe(result => console.log('Result', result)); | |
// => Result 42 | |
// => Result 56 | |
// => Result 72 | |
// Catch | |
Rx.Observable | |
.catch( | |
Promise.reject(new Error('woops')), | |
Promise.resolve(42) | |
) | |
.subscribe(result => console.log('Result', result)); | |
// => Result 42 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment