Skip to content

Instantly share code, notes, and snippets.

@guillefd
Last active January 29, 2019 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guillefd/7c98b53626337111287bee6e98dc264a to your computer and use it in GitHub Desktop.
Save guillefd/7c98b53626337111287bee6e98dc264a to your computer and use it in GitHub Desktop.
Get a list, perform a fetch on each item and return one array of objects as result
/**
Returns a list of objects
1. get the list of files to fetch
2. fetch each file to get the object
3. return an array of objects
*/
getAll(): Observable<Tutorial[]> {
const list = this.dataSrv.getDataList()
.pipe(
// get list from dataList
map(dataList => dataList.list),
// merge all observables in one
mergeMap(list => {
// call all observables in parallel
return forkJoin(list.map(file => {
// get object as observable
return this.dataSrv.getData(file)
}))
})
)
return list;
}
/* Simplified */
getAll(): Observable<Tutorial[]> {
return this.dataSrv.getTutorialsData()
.pipe(
map(dataList => dataList.list),
mergeMap(list => forkJoin(list.map(file => this.dataSrv.getTutorialData(file))))
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment