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

153
Vistas
Get value from setTimout inside a forEach

I have now spent several hours here reading through the solutions to this problem, like this: Get return value from setTimeout

But I can't find any solution for my problem to get the removeCount value. I also tried to add a Promise, but I don't know how to do it with the increment.

async function removeNeedlessNotifications(mutations) {
    const getResult = async () => {
        let removeCount = 0;
        mutations.forEach((v) =>
            v.addedNodes.forEach((v, i) =>
                setTimeout(() => {
                    if (notificationFilter(v)) {
                        v.querySelector("div.activity-remove.js-remove").click();
                        removeCount++;
                    }
                }, i * 100)
            )
        );
        return removeCount;
    };

    return await getResult();
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use two for loops instead so you can await inside, and it becomes trivial:

async function removeNeedlessNotifications(mutations) {
    let removeCount = 0;
    for (const mutation of mutations) {
        for (const node of mutation.addedNodes) {
            await new Promise(r => setTimeout(r, 100));
            if (notificationFilter(node)) {
                node.querySelector("div.activity-remove.js-remove").click();
                removeCount++;
            }
        }
    }
    return removeCount;
}

This will return a Promise that resolves to the number of divs clicked.

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