• Creates a derivative from an asynchronous computation.

    An update to the derivative is postponed until the promise is settled, and the resulting value or error is then used. In the meantime, the value of the derivative will be the previously computed value, or undefined when first created.

    If any dependencies change before an outstanding promise settles, the outstanding promise will be ignored and the new promise will be used.

    Parameters

    • deps: any
    • async_func: any

    Returns any

    Example

    let value = createSignal("example");

    let hashedValue = createEventual({ createSignal, createEffect }, async () => {
    let hash = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(value()));
    return Array.from(new Uint8Array(hash)).map(b => b.toString(16).padStart(2, "0")).join("");
    });

    createEffect(() => console.log(hashedValue()));

    Description

    It is necessary to access dependencies immediately at the start of the function (in the same microtask) to ensure future changes to that dependency are detected. For example, the derivative in the example below will never be updated when the dependency changes. Alternatively, consider using createEventual(createDynamic(async () => ...)).

    Example

    let value = createSignal(0);

    let double = createEventual({ createSignal, createEffect }, async () => {
    await "nothing";
    return 2 * value();
    });

    createEffect(() => console.log(double())); // this effect will only ever run once

Generated using TypeDoc