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

86
Visualizações
Javascript rerun the function until condition is false

Via API, I would like to list out the whole users. Each time can only have maximum 100 items for each page. So that I need to get the next_page url to rerun the function - runPages to collect the list. So the condition is when the next_page == null, then will stop the function.

In my code, I can get the next_page url. However, it doesn't run further. Can someone figure out what is the problem?

const runPages = async (values) => {
  if (values.next_page != null) {
    for (const field of values.results) {
      row.appendChild(addCell(field.name));
      row.appendChild(addCell(field.email));
      tblbody.appendChild(row);
    }
    values = await checkPages(values.next_page); // get new values.data by url
    runPages(values);
  }
};
runPages(values);

const checkPages = async (value) => {
  return new Promise((resolve, reject) => {
    const getNewPageFromApi = async () => {
      const GetUrl = `${value}`;
      const Doorkey = { username: "XXX", password: "*****" };
      try {
        const response = await Axios.get(GetUrl, { auth: Doorkey });
        if (response.data.next_page != null) {
          resolve(response.data);
        }
      } catch (err) {
        reject("no more data");
      }
    };
    getNewPageFromApi();
  });
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I don't know if this is the answer you're looking for but since you're checking for values.next_page != null in the runPages function you can call the resolve(response.data) directly in the Promise inside checkPages function without checking for response.data.next_page != null.

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