Skip to main content
Version: 11.14.0

withFetch

withFetch wraps an async callback in Angular's injection context and transparently handles common HTTP error responses. 401 errors redirect to the login page; 404 errors return undefined silently.

API

withFetch<T>(callback: () => Promise<T>, injector?: Injector): Promise<T | undefined>
NameTypeRequiredDescription
callback() => Promise<T>The async operation to execute.
injectorInjectorAn optional Angular injector. When omitted, the current injection context is used.

Returns Promise<T | undefined> — the resolved value, or undefined if a 404 was thrown.

Example

example.component.ts
import { inject } from '@angular/core';
import { withFetch } from '@sinequa/atomic-angular';

const data = await withFetch(() => fetch('/api/resource').then(r => r.json()));