Last active
May 15, 2016 17:48
-
-
Save mattpodwysocki/9590917 to your computer and use it in GitHub Desktop.
New support with Promises for RxJS
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
// Observable to Promise | |
var promise = Rx.Observable.return(42).toPromise(RSVP.Promise); | |
promise.then(console.log.bind(console)); | |
// => 42 | |
// Using config instead of argument | |
Rx.config.Promise = RSVP.Promise; | |
var promise = Rx.Observable.return(42).toPromise(); | |
promise.then(console.log.bind(console)); | |
// => 42 | |
// Converting Promise to Observable | |
var observable = RSVP.Promise.resolve(42); | |
observable.subscribe(console.log.bind(console)); | |
// => 42 | |
// Using flatMap/selectMany with a promise | |
var observable = Rx.Observable.return(42) | |
.flatMap(RSVP.Promise.resolve(56)); | |
observable.subscribe(console.log.bind(console)); | |
// Using flatMap/selectMany with a promise inside a function | |
var observable = Rx.Observable.return(42) | |
.flatMap(function (x, i) { return RSVP.Promise.resolve(x + 1); }); | |
observable.subscribe(console.log.bind(console)); | |
// => 43 | |
// Using flatMap/selectMany with a promise inside a function with a result selector | |
var observable = Rx.Observable.return(42) | |
.flatMap( | |
function (x, i) { return RSVP.Promise.resolve(x + 1); }, | |
function (x, y, i) { return { fst: x + i, snd: y }}); | |
observable.subscribe(console.log.bind(console)); | |
// => { fst: 42, snd: 43 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment