Skip to content
Discussion options

You must be logged in to vote

I would not rely on this pattern. A computed getter should be pure: it should derive and return a value from existing reactive state, not create new refs, start promises, or mutate state later from inside the getter.

In your example, the getter does all three:

const foo = computed(() => {
  const num = ref(0);

  const modifyPromise = new Promise<void>((resolve) => {
    setTimeout(() => {
      num.value++;
      resolve();
    }, 1000);
  });

  return {
    num,
    modifyPromise,
  };
});

That makes the result dependent on when Vue decides to evaluate or re evaluate the computed. In SSR this is especially fragile because server rendering, onServerPrefetch, async dependency resolution,…

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@illusionaries
Comment options

Comment options

You must be logged in to vote
2 replies
@illusionaries
Comment options

@GitNimay
Comment options

Answer selected by illusionaries
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants