Skip to content

Instantly share code, notes, and snippets.

@BerkeleyTrue
Created March 12, 2017 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BerkeleyTrue/1556c5f3748ee3d8d7fef02dcd04ca31 to your computer and use it in GitHub Desktop.
Save BerkeleyTrue/1556c5f3748ee3d8d7fef02dcd04ca31 to your computer and use it in GitHub Desktop.
const serviceValues = [ 2, 3, 4 ];
// convert values array into a stream of individual values: Obs[...num]
return Observable.of(serviceValues)
// call service on each value and get Observable in return: Obs[Obs[newValFromService]]
.map(val => serviceApi(val))
// convert stream of observables into a stream of a single array of observables Obs[ [Obs[newVal]] ]
.toArray()
// trigger all the service calls with maximum concurrenct
.switchMap((serviceCalls) => Observable.combineLatest(serviceCalls))
.subscribe(newVals => console.log('new values from server: ', newVals)) // [...{id: 2, ...data}, {id: 3, ...dat}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment