Skip to content

Why use setTimeout trigger effect rather than queueMicrotask #465

@cubxx

Description

@cubxx

I found the update is always slow by 1 frame when I change state in the rAF callback:

//minimal case
const a = van.state(0);
van.dervie(() => (el.textContent = a.val));
requestAnimationFrame(async (time) => {
  a.val = time;
  await 0; // wait next microtask
  console.log(time === el.textContent); // expect true but got false
});

I know it is because the setTimeout callback will be called after the render pipeline, but updating the DOM in time in the rAF callback is usual practice.
I found that the other reactive systems usually update on the next microtask, which ensures that the update will occur before the render pipeline.

https://jsfiddle.net/akqjhcy6/
https://playground.solidjs.com/anonymous/01307c7d-763d-4d87-a00b-432f12b85936

So, is there any reason why setTimeout must be used?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions