Skip to content

Instantly share code, notes, and snippets.

@alexzuza
Created August 28, 2019 16:52
Show Gist options
  • Save alexzuza/c445e78604708c4ba64dadb7df166918 to your computer and use it in GitHub Desktop.
Save alexzuza/c445e78604708c4ba64dadb7df166918 to your computer and use it in GitHub Desktop.
With loading pipe
import { Pipe, PipeTransform } from '@angular/core';
import { isObservable, of } from 'rxjs';
import { map, startWith, catchError } from 'rxjs/operators';
@Pipe({
name: 'withLoading',
})
export class WithLoadingPipe implements PipeTransform {
transform(val) {
return isObservable(val)
? val.pipe(
map((value: any) => ({ loading: false, value })),
startWith({ loading: true }),
catchError(error => of({ loading: false, error }))
)
: val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment