Last active
January 29, 2019 13:53
-
-
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
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
/** | |
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