How to ensure all screenshots finish processing before closing Puppeteer browser?

I’m working on a project where I need to capture screenshots every 20 milliseconds while a webpage is loading in Puppeteer. The problem I’m facing is that taking screenshots happens really quickly, but actually saving the image files takes much longer. When I close the browser, many of the screenshot files haven’t finished being written to disk yet, so they get lost. I need a way to make sure all the screenshot operations are completely done before the browser shuts down. Has anyone dealt with this timing issue before? What’s the best approach to wait for all pending screenshot saves to complete?

Had the same issue with a web scraping tool that took tons of screenshots. The problem? writeFile is async but most people don’t await it properly. Here’s what fixed it for me: I built a queue system that tracks each screenshot operation with a counter. Before closing the browser, I added polling to check if the queue’s empty and everything’s done. You can also wrap your screenshot saving in a promise that only resolves when the file actually hits disk, then keep references to all those promises until they’re finished.

track your screenshot promises in an array, then use promise.all() to wait for all to finish b4 closing puppeteer. just await Promise.all(screenshotPromises) and you should be good!

Had this exact problem building a performance monitoring tool. Puppeteer’s screenshot method returns right away, but the actual file writing happens async in the background. Here’s what fixed it for me: use a semaphore to limit concurrent screenshots and switch to fs.promises.writeFile instead of callbacks. I wrapped each screenshot in a function that only resolves after the buffer’s actually written to disk. Before calling browser.close(), just await all pending operations. This stops the race condition where the browser dies before files finish writing. The slight performance hit from limiting concurrency is nothing compared to losing screenshots.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.