Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Last active May 15, 2016 17:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/9590917 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/9590917 to your computer and use it in GitHub Desktop.
New support with Promises for RxJS
// 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