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

157
Visualizações
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 Respostas
Responde à pergunta

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 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