Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

204
Visualizações
Vue 2 - promise not resolving after routing to different page

I know this question may sound repetitive, but I've been searching for a solution for a long time, and couldn't find any.

I have a method that resolves a promise after the window is loaded. Later, I am waiting for that method in my mounted hook. When routing to that page, neither the promise itself, nor anything after it is executed. However, if I refresh the page, everything runs smoothly.

As an example, this is my method:

getPosition() {
    return new Promise((resolve, reject) => {
        window.addEventListener("load", () => {
            console.log("window is loaded");
            resolve();
        });
    });
},

This is my mounted hook:

async mounted() {
    console.log("before promise");  // this logs
    await this.getPosition();       // this does not log
    console.log("after promise");   // this does not log
},
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

getPosition is not guaranteed to be executed before load, and race condition will result in pending promise. That a window has already been loaded should be additionally checked:

new Promise(resolve => {
    if (document.readyState === 'complete') {
      resolve();
      return;
    }

    window.addEventListener("load", () => resolve());
});

load event happens after loading all images and styles, which is not usually needed but results in additional delay. Most times it's useful to wait for DOM instead:

new Promise(resolve => {
    if (document.readyState !== 'loading') {
      resolve();
      return;
    }

    document.addEventListener("DOMContentLoaded", () => resolve());
});

load and DOMContentLoaded event are expected to be emitted once on initial page load. Subsequent DOM changes and network requests aren't supposed to be tracked through them.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda