Skip to main content
cngx-src documentation

tapHttpAsyncState

Functioncommon/data/async-state

projects/common/data/async-state/operators.ts

Description#

RxJS operator that combines tapAsyncState + tapAsyncProgress for HTTP event streams.

Pipe this onto an HttpClient call with { observe: 'events', reportProgress: true }. It will:

  1. Set loading on subscribe
  2. Report upload/download progress via setProgress()
  3. Extract the response body on HttpEventType.Response
  4. Call setSuccess(body) with the extracted body
  5. Call setError(err) on error

The output Observable emits the response body (not HttpEvents). Throws if the response body is null.

readonly upload = createManualState<UploadResult>();

handleUpload(file: File): void {
  this.http.post('/api/upload', file, {
    reportProgress: true,
    observe: 'events',
  }).pipe(
    tapHttpAsyncState(this.upload),
    takeUntilDestroyed(this.destroyRef),
  ).subscribe();
  // upload.status(), upload.progress(), upload.data() — all wired
}

Signature#

tapHttpAsyncState(state: AsyncStateSink, options?)

Parameters#

@paramstateAsyncStateSink
@paramoptions?

Returns#

OperatorFunction