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

155
Vistas
Calling a function multiple times in a while loop

I am trying to get data from a function call and I am keeping it in a while loop so the function will be called again and again if data is not received. What I meant with the data is the data1,data2 and data3 arrays that I have returned from the getData function should be populated and not have a null value. But I am not able to find the solution.

Here is my code :

router.get('/someurl' , async(req,res) => {
let data = [];
while(data.length == 0)
{
  data = await functionCall()
  console.log(data)
   }
})

And here is the format of my functionCall code :

const unirest = require("unirest");
const cheerio = require('cheerio')





const getData = async() => {
   const data1 = [] , data2 = [] , data3 = [] ;
   try {
   const response = await unirest
   .get('url')
   .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
   .proxy("proxy")
   const $ = cheerio.load(response.body)
   $('hided').each((i,el) =>
   {
         data1[i] = $(el)
         .find('hided')
         .text()
         data2[i] = $(el)
         .find('hided')
   })
   $('hided').each((i,el) =>
   {
         data3[i] = $(el)
         .find('hided')
         .text()
   })
    if(data1.length && data2.length && data3.length))
    {
      return [data1 , data2 , data3]
    }
   }
      catch(error)
      {
         console.log("Error" , error);
      }
      return [];
      }
 
 
module.exports =  getData
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Assuming that fetchData is an API call there's no reason to initially define data. You can just log the results of calling that API until the results are an empty array.

const data = [1, 2, 3, 4];

// A simple API that returns a reduced array
// on every call
function mockApi() {
  return new Promise(res => {
    setTimeout(() => {
      res([...data]);
      data.shift();
    }, 1000);
  });
}

// Call the API, and if the the response
// has zero length log "No data" otherwise
// log the data, and call the function again.
async function fetchData() {
  const data = await mockApi();
  if (!data.length) {
    console.log('No data');
  } else {
    console.log(data);
    fetchData();
  }
}

fetchData();

about 4 years ago · Juan Pablo Isaza Denunciar

0

Firstly remove the if statement, as it is literally what the while loop is doing. then remove await, as this is not in an async function, and it will make an error.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try something like this.

const fetchData = async ()=>{
data = await functionCall()
if(data.length==0){
fetchData()
}else{
return}
}
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