AniLink - v2.0.0
    Preparing search index...

    Function fetchWithLookAhead

    • Shared look-ahead driver for paged traversals.

      Fetches entries through a sliding window of at most concurrency launched- but-unconsumed requests so round-trip latency overlaps instead of stacking, while results are appended strictly in entry order no matter when each request settles. Scheduling stops as soon as an entry reports "no more data" (per the caller-supplied extractHasMore) or the maxEntries guard fires; truncated mirrors the sequential semantics.

      Because the window runs ahead of consumption, up to concurrency - 1 already-launched requests may complete past a terminal entry; their payloads are drained and discarded so the collected prefix matches what a strictly sequential traversal would have returned.

      Two call shapes are supported:

      1. Numeric paging (AniList pages and chunks): fetchWithLookAhead(fetch, extractHasMore, undefined, startNumber, maxEntries, concurrency) or the shorthand five-argument form fetchWithLookAhead(fetch, extractHasMore, startNumber, maxEntries, concurrency). Keys advance by slot arithmetic (startNumber + slot).

      2. Cursor paging (MyAnimeList and any provider whose next key is carried by the previous response): pass an extractNextKey callback as the third argument. Each consumed entry supplies the key for its successor; the first key is firstKey. Cursor mode never schedules past a terminal entry even if that entry still carries a stale next key.

      Type Parameters

      • TEntry

        The raw response shape of a single page or chunk.

      • TKey = number

        The paging key type: a page number in numeric mode, or an opaque cursor value in cursor mode.

      Parameters

      • fetch: (key: TKey) => Promise<TEntry>

        Callback that fetches a single entry given its paging key.

      • extractHasMore: (response: TEntry) => boolean

        Reads the "more data available" flag from a fetched entry. Return false for malformed responses so a broken payload ends the traversal instead of looping forever.

      • ...rest:
            | [startNumber: number, maxEntries: number, concurrency: number]
            | [
                extractNextKey: (response: TEntry) => TKey,
                firstKey: TKey,
                maxEntries: number,
                concurrency: number,
            ]

        Positional paging arguments in one of two shapes: numeric paging [startNumber, maxEntries, concurrency], or cursor paging [extractNextKey, firstKey, maxEntries, concurrency] where extractNextKey reads the next paging key from a fetched entry (undefined selects numeric paging). Keys, caps, and the concurrency window are already resolved and validated when this driver is invoked.

      Returns Promise<LookAheadResult<TEntry>>

      The responses in entry order, how many were fetched, and whether the guard truncated the run.

      The rejection from the next unconsumed fetch call in entry order.