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>
);
}
// 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>
);
}
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?