Skip to content
module@warp-drive/core/reactive

Function getRequestState() ​

ts
function getRequestState<RT, E>(future: Future<RT>): Readonly<RequestState<RT, StructuredErrorDocument<E>>>;

Defined in: warp-drive-packages/core/src/signals/request-state.ts:855

getRequestState can be used in both JavaScript and Template contexts.

It returns a RequestState: a reactive object that updates as the request advances. Calling it again with the same Future returns the same object. Check status to narrow it to one of four states:

  • PendingRequest ('pending') is the initial state, before the request settles.
  • ResolvedRequest ('fulfilled') holds the response content on value.
  • RejectedRequest ('rejected') holds the error on reason.
  • CancelledRequest ('cancelled') is a request that was aborted. isError is also true for a cancelled request, so check status or isCancelled rather than isError if you handle it differently.

Every state also exposes a RequestLoadingState on loadingState for tracking the progress of the response stream.

The Reactive Control Flow guide shows how to render request states with it, and Using The Response covers the Future it reads.

ts
import { getRequestState } from '@warp-drive/ember';

const state = getRequestState(future);

For instance, we could write a getter on a component that updates whenever the request state advances or the future changes, by combining the function with the use of @cached

ts
class Component {
  @cached
  get title() {
    const state = getRequestState(this.args.request);
    if (state.isPending) {
      return 'loading...';
    }
    if (state.isError) { return null; }
    return state.result.title;
  }
}

Or in a template as a helper:

gjs
import { getRequestState } from '@warp-drive/ember';

<template>
  {{#let (getRequestState @request) as |state|}}
    {{#if state.isPending}}
      <Spinner />
    {{else if state.isError}}
      <ErrorForm @error={{state.error}} />
    {{else}}
      <h1>{{state.result.title}}</h1>
    {{/if}}
  {{/let}}
</template>

If looking to use in a template, consider also the <Request /> component which offers a number of additional capabilities for requests beyond what RequestState provides.

Type Parameters ​

RT ​

RT

E ​

E

Parameters ​

future ​

Future<RT>

the request Future to track, as returned by store.request or requestManager.request

Returns ​

Readonly<RequestState<RT, StructuredErrorDocument<E>>>

the cached RequestState for future

Released under the MIT License.