Logos DX
    Preparing search index...

    Class FetchPromise<T, H, P, RH>

    Extended Promise that carries a response directive for the fetch engine.

    Enables a fluent API where callers declare their expected response type (e.g. .json(), .text(), .blob()) before awaiting. The directive is read by the executor to determine how to parse the response body.

    An override guard prevents setting the directive more than once, catching accidental double-calls that would silently discard the first directive.

    const user = await api.get('/users/1').json();
    const html = await api.get('/page').text();
    const file = await api.get('/file').blob();

    Type Parameters

    • T = unknown

      Type of the parsed response data

    • H = unknown

      Type of request headers

    • P = unknown

      Type of request params

    • RH = unknown

      Type of response headers

    Hierarchy (View Summary)

    Index

    Constructors

    • Creates a new Promise.

      Type Parameters

      • T = unknown

        Type of the parsed response data

      • H = unknown

        Type of request headers

      • P = unknown

        Type of request params

      • RH = unknown

        Type of response headers

      Parameters

      • executor: (
            resolve: (
                value:
                    | FetchResponse<T, H, P, RH>
                    | PromiseLike<FetchResponse<T, H, P, RH>>,
            ) => void,
            reject: (reason?: any) => void,
        ) => void

        A callback used to initialize the promise. This callback is passed two arguments: a resolve callback used to resolve the promise with a value or the result of another promise, and a reject callback used to reject the promise with a provided reason or error.

      Returns FetchPromise<T>

    Properties

    "[toStringTag]": string
    "[species]": PromiseConstructor

    Accessors

    • get directive(): ResponseDirective | undefined

      The active response directive, if any.

      Returns ResponseDirective | undefined

    • get isAborted(): boolean

      Whether the request was aborted, either via abort() or an external signal on the controller.

      Returns boolean

    • get isFinished(): boolean

      Whether the executor has resolved or rejected (without abort).

      Returns boolean

    • get isStream(): boolean

      Whether stream mode is active.

      Returns boolean

    Methods

    • Async iterator that yields response body chunks as Uint8Array.

      Only available after calling .stream(). Reads from the underlying ReadableStream and releases the reader lock when iteration ends.

      Returns AsyncIterator<Uint8Array<ArrayBufferLike>>

      for await (const chunk of api.get('/large-file').stream()) {
      process(chunk);
      }
    • Abort the in-flight request.

      Sets isAborted and delegates to the stored AbortController. Safe to call even when no controller is present (plain constructor).

      Parameters

      • Optionalreason: string

      Returns void

    • Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.

      Parameters

      • Optionalonfinally: (() => void) | null

        The callback to execute when the Promise is settled (fulfilled or rejected).

      Returns Promise<FetchResponse<T, H, P, RH>>

      A Promise for the completion of the callback.

    • Enable streaming mode for the response.

      Returns the raw Response and marks the promise for async iteration over response body chunks.

      Returns FetchStreamPromise<H, P, RH>

    • Parse the response body as plain text.

      Returns FetchPromise<string, H, P, RH>

    • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

      Type Parameters

      • T

      Parameters

      Returns Promise<Awaited<T>[]>

      A new Promise.

    • Creates a Promise that is resolved with an array of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • values: T

        An array of Promises.

      Returns Promise<{ -readonly [P in string | number | symbol]: Awaited<T[P]> }>

      A new Promise.

    • The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • values: T

        An array or iterable of Promises.

      Returns Promise<Awaited<T[number]>>

      A new Promise.

    • The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.

      Type Parameters

      • T

      Parameters

      Returns Promise<Awaited<T>>

      A new Promise.

    • Factory that wires an executor and AbortController into a FetchPromise with automatic finish/abort tracking.

      Replaces the previous pattern of patching abort state onto a plain promise, giving callers a typed, first-class abort surface.

      Type Parameters

      • T
      • H
      • P
      • RH

      Parameters

      Returns FetchPromise<T, H, P, RH>

      const controller = new AbortController();
      const fp = FetchPromise.create(
      () => engine.execute(request, controller.signal),
      controller
      );
      fp.abort('user cancelled');
    • Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.

      Type Parameters

      • T

      Parameters

      Returns Promise<Awaited<T>>

      A new Promise.

    • Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.

      Type Parameters

      • T extends [] | readonly unknown[]

      Parameters

      • values: T

        An array of Promises.

      Returns Promise<Awaited<T[number]>>

      A new Promise.

    • Creates a new rejected promise for the provided reason.

      Type Parameters

      • T = never

      Parameters

      • Optionalreason: any

        The reason the promise was rejected.

      Returns Promise<T>

      A new rejected Promise.

    • Takes a callback of any kind (returns or throws, synchronously or asynchronously) and wraps its result in a Promise.

      Type Parameters

      • T
      • U extends unknown[]

      Parameters

      • callbackFn: (...args: U) => T | PromiseLike<T>

        A function that is called synchronously. It can do anything: either return a value, throw an error, or return a promise.

      • ...args: U

        Additional arguments, that will be passed to the callback.

      Returns Promise<Awaited<T>>

      A Promise that is:

      • Already fulfilled, if the callback synchronously returns a value.
      • Already rejected, if the callback synchronously throws an error.
      • Asynchronously fulfilled or rejected, if the callback returns a promise.
    • Creates a new Promise and returns it in an object, along with its resolve and reject functions.

      Type Parameters

      • T

      Returns PromiseWithResolvers<T>

      An object with the properties promise, resolve, and reject.

      const { promise, resolve, reject } = Promise.withResolvers<T>();