Skip to content

Instantly share code, notes, and snippets.

@studioTeaTwo
Created September 9, 2019 16:56
Show Gist options
  • Save studioTeaTwo/f23f7bfed249ffa168330d48ec271a3a to your computer and use it in GitHub Desktop.
Save studioTeaTwo/f23f7bfed249ffa168330d48ec271a3a to your computer and use it in GitHub Desktop.
return immediately if source response empty array. otherwise adding some rx functions.
// return immediately empty if source response empty array.
// otherwise adding some functions.
const Emptialbe = <T>(source: Observable<T[]>, additional: Observable<T[]>) => {
source.pipe(
switchMap(array => {
if (array.length === 0) {
return of(array)
} else {
return additional(array)
}
})
)
}
// example
const someApiResponse$ = someApi();
const anotherApiResponse$ = anotherApi();
Emptialbe<string>(apiResponse$, (array) => forkJoin(of(array), anotherApiResponse$)
.pipe(
map([someApiResponse, anotherApiResponse] => {
return mappedValue;
})
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment