Skip to content

0.1.28 - Async Iterator Support

Choose a tag to compare

@ihasq ihasq released this 25 Jul 07:27
· 62 commits to main since this release

💫 Async Iterator Support

For customized complex fallbacks:

import { $, h as html } from "hstd";

async function* UserProfile(userId: string): AsyncGenerator<HSTDFragment> {

    const
        loadingDots: $<number> = $(0, $ => $ % 3),
        loadingDotsInterval: string = setInterval(() => loadingDots.$++, 700)
    ;

    yield html`
        <span>Loading${$`.`.repeat(loadingDots.into($ => $ + 1))}</span>
    `;
    
    const { name, age, link } = await fetch(`/api/user/${userId}`).then(res => res.json());

    clearInterval(loadingDotsInterval);

    yield html`
        <div>
            <label>${name}</label>
            <label>${age}</label>
            <label>${link}</label>
        </div>
    `;

}

document.body.append(...html`${UserProfile("ihasq")}`);

or write your own Suspense:

async function* Suspense({ fallback }, main) {
    yield* fallback;
    yield* await main;
}

document.body.append(...html`
    ${Suspense({ fallback: html`<label>Loading...</label>` },
        User({ id: "ihasq" })
    )}
`)