SolidJSS
SolidJSโ€ข9mo ago
PureSoul

when in createAsync with deferStream, if I update state, it not immediatly updating createMemo.

// ExampleSolidAsync.tsx
import { createSignal, createMemo, createAsync } from "solid-js";

export default function ExampleSolidAsync() {
  const [count, setCount] = createSignal(0);

  const doubled = createMemo(() => {
    console.log("Memo re-running, count:", count());
    return count() * 2;
  });

  const asyncData = createAsync(async () => {
    console.log("Before setCount, count:", count(), "doubled:", doubled());

    setCount(10); // updating count
    console.log("After setCount, count:", count(), "doubled:", doubled());

    await Promise.resolve();
    console.log("After await Promise.resolve(), count:", count(), "doubled:", doubled());

    await new Promise((r) => setTimeout(r, 1000));
    console.log("After 1 second, count:", count(), "doubled:", doubled());

    return "done";
  });

  return (
    <div>
      <h1>Count: {count()}</h1>
      <h2>Doubled: {doubled()}</h2>
      <h3>Async result: {asyncData()}</h3>
    </div>
  );
}
Was this page helpful?