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

331
Visualizações
Why do I need to to use resolve() when calling a promise recursively

I'm a bit confused about promises in javascript and the resolve() statement. I have this code

this.fetchRuns("https://www.speedrun.com/api/v1/runs?game=" + game_id).
then(() => console.log("abc"));

fetchRuns(url, max);
{
  return new Promise((resolve, reject) => {
    if (this.runs.length >= 100) return resolve();
    fetch(url)
      .then((data) => data.json())
      .then((json_obj) => {
        json_obj["data"].forEach((element) => {
          this.runs.push(element);
        });
        if (json_obj["pagination"]["links"].length == 0) return resolve();
        else {
          if (json_obj["pagination"]["links"].length == 1)
            resolve(this.fetchRuns(json_obj["pagination"]["links"][0]["uri"]));
          else resolve(this.fetchRuns(json_obj["pagination"]["links"][1]["uri"]));
        }
      });
  });
}

When I recursively call the function again, I need to use resolve() so that console.log("abc") gets called. I want to know the reason why.

My own thought is that every promise needs to be resolved so that then() will be called and when I don't use resolve() in the recursive call then the promise doesn't get resolved. Am I right with that?

about 4 years ago · Juan Pablo Isaza
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