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

190
Vistas
Delay before recursive function call

I'm trying to set a delay before every recursive function call. Currently, it's returning undefined. The problem might be the scope of the recursive call (inside settimeout and then). I tried it like this:

    function delay(ms) {
        return new Promise(resolve => {
            setTimeout(resolve, ms)
        });
    } 

    const checkIfListElementIsRendered = (className) => {
        delay(10).then(function (res) {
            if (document.getElementsByClassName(className)[0]) {
                return true;
            }
            return checkIfListElementIsRendered(className);
        })
    }

And this:

    const checkIfListElementIsRendered = (className) => {
        if (document.getElementsByClassName(className)[0]) {
            return true;
        }
            
        setTimeout(() => {
            return checkIfListElementIsRendered(className);
        }, 10);   
    }

Any help is appreciated!

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You need to turn checkIfListElementIsRendered to async/await syntax, and await the delay and checkIfListElementIsRendered methods, Also instead return true turn it to promise to be always return a promise.

When you call checkIfListElementIsRendered method outside, you need to resolve it with await or then callback.

function delay(ms) {
    return new Promise(resolve => {
        setTimeout(resolve, ms)
    });
} 

const checkIfListElementIsRendered = async (className) => {
   await delay(10)
   if (document.getElementsByClassName(className)[0]) {
      return Promise.resolve(true);
   }
   return await checkIfListElementIsRendered(className);
}
about 4 years ago · Santiago Trujillo 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