Search code examples
javascriptasynchronousforeachsettimeout

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();
}

Solution

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