Wait until Pinia is loaded

Viewed 31

Immediately launched code getting data from backend and tries to save them in Pinia. But got an error

getActivePinia was called with no active Pinia. Did you forget to install pinia?

Question: Can I create a function to check if Pinia already ready or not? Is that any flag I can check?

I expected to create a function like this, to check Pinia status in a given amount of time

function isPiniaReady() {
  let time = 0;
  const timeLimit = 300
  const interval = 30;

  return new Promise((resolve, reject) => {
    const index = setInterval(() => {

      // success
      if (....<pinia is ready>....) {  // <- how to write this condition?
        clearInterval(index);
        return resolve(true);
      }

      // fail
      if (time >= timeLimit) {
        clearInterval(index);
        return reject(new Error('Pinia is never ready'));
      }

      time += interval;
    }, interval)
  })
}
1 Answers
Related