# core/loaders/payload.loader.ts

> 31 lines of code and 7 definitions.

Tree: Site tree
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/tree#file-core-loaders-payload-loader-ts
Source text: https://banes-lab.com/assets/sources/source.c2782a8a1d5382b7d1c312657fffd050b17f6e1eefa4be93282924ecec2b330d.generated.txt

## Definitions

- `fetchPayload` (lexical_declaration, line 6)
- `held` (lexical_declaration, line 4)
- `response` (lexical_declaration, line 8)
- `data` (lexical_declaration, line 13)
- `loadPayload` (lexical_declaration, line 21, exported)
- `pending` (lexical_declaration, line 22, exported)
- `loading` (lexical_declaration, line 26, exported)

## Uses

- [core/reporters/failure.reporter.ts](https://banes-lab.com/source/tree/core/reporters/failure.reporter.ts.md)

## Source

```typescript
import { PAYLOAD_UNAVAILABLE } from "#configuration/strings/report.strings";
import { reportFailure } from "#core/reporters/failure.reporter";

const held = new Map<string, Promise<unknown>>();

const fetchPayload = async function fetchPayload(path: string): Promise<unknown> {
    try {
        const response = await fetch(path);
        if (!response.ok) {
            reportFailure(PAYLOAD_UNAVAILABLE, path);
            return null;
        }
        const data: unknown = await response.json();
        return data;
    } catch (error) {
        reportFailure(PAYLOAD_UNAVAILABLE, error);
        return null;
    }
};

export const loadPayload = async function loadPayload(path: string): Promise<unknown> {
    const pending = held.get(path);
    if (pending !== undefined) {
        return pending;
    }
    const loading = fetchPayload(path).then((payload) => {
        if (payload === null) {
            held.delete(path);
        }
        return payload;
    });
    held.set(path, loading);
    return loading;
};
```
