Last active
November 3, 2018 09:33
-
-
Save mattpodwysocki/9593643 to your computer and use it in GitHub Desktop.
New support for Promises in RxJS to include conversion to and from Promises, and operator support such as: flatMap/selectMany, mergeObservable/mergeAll, concatObservable/concatAll, switchLatest/switch, and startAsync
This file contains hidden or 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 } | |
// Concat support | |
var sources = Rx.Observable | |
.fromArray([ | |
RSVP.Promise.resolve(42), | |
RSVP.Promise.resolve(56), | |
RSVP.Promise.resolve(72)] | |
) | |
.concatAll(); | |
sources.subscribe(console.log.bind(console)); | |
// => 42 | |
// => 56 | |
// => 72 | |
// Merge support | |
var sources = Rx.Observable | |
.fromArray([ | |
RSVP.Promise.resolve(42), | |
RSVP.Promise.resolve(56), | |
RSVP.Promise.resolve(72)] | |
) | |
.mergeAll(); | |
sources.subscribe(console.log.bind(console)); | |
// => 42 | |
// => 56 | |
// => 72 | |
// Switch support | |
var sources = Rx.Observable | |
.fromArray([ | |
RSVP.Promise.resolve(42), | |
RSVP.Promise.resolve(56), | |
RSVP.Promise.resolve(72)] | |
) | |
.switch(); | |
sources.subscribe(console.log.bind(console)); | |
// => 72 | |
// StartAsync Support | |
var source = Rx.Observable.startAsync(function () { | |
return RSVP.Promise.resolve(42); | |
}); | |
source.subscribe(console.log.bind(console)); | |
// => 42 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment