Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

85
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda