Created
March 14, 2017 01:44
-
-
Save slmyers/a0f81f1478c4dd50845b7637abb4e773 to your computer and use it in GitHub Desktop.
Observable scheduler examles
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
import * as Rx from 'rxjs/Rx'; | |
function getVal(arr1, arr2){ | |
const obs = Rx.Observable.combineLatest( | |
Rx.Observable.from(arr1), Rx.Observable.from(arr2) | |
) | |
//.let(filterFunc) // defined elsewhere assume somewhat complicated logic | |
.reduce( (accum, val) => [...accum, val], [] ) | |
.catch( err => Rx.Observable.of(`an error while filtering ${err.data}`)) | |
let combined; | |
obs.subscribe( | |
arr => combined = arr, | |
error => { | |
console.error(error); | |
combined = []; | |
} | |
) | |
return combined; | |
} | |
function getAsyncVal(arr1, arr2){ | |
const wrap = arr => new Promise( resolve => resolve(arr)); | |
const obs = Rx.Observable.combineLatest( | |
Rx.Observable.fromPromise(wrap(arr1)), | |
Rx.Observable.fromPromise(wrap(arr2)) | |
) | |
.reduce( (accum, val) => [...accum, val], [] ) | |
.catch( err => Rx.Observable.of(`an error while filtering ${err.data}`)) | |
let combined; | |
obs.subscribe( | |
arr => combined = arr, | |
error => { | |
console.error(error); | |
combined = []; | |
} | |
) | |
return combined; | |
} | |
console.log(getVal([1,2,3], [3,2,1])); | |
console.log(getAsyncVal([1,2,3], [3,2,1])); | |
/* output */ | |
// [ [ 3, 3 ], [ 3, 2 ], [ 3, 1 ] ] | |
// undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment