I have a puppeteer program, in which I have to call page.waitForSelector, which is an asynchronous function, to see if it throws any errors. But I assume, because a try-block is synchronous and it doesn't wait for an asynchronous function to be finished, even when I use await, it continues Executing the code after the try-block before even checking if there's an error. This is my code:
try {
await page.waitForSelector("#captchaImage", {timeout: 6000});
}
catch {
console.log("Your URL has changed");
return;
}
console.log("You're still on the good site :)");
Is there a way to fix this problem or to make an asynchronous function synchronous (So it blocks code execution)?
page.waitForSelectormust return a Promise (otherwise you couldn'tawaitit), so why not just use the built-in catch method rather than a try/catch which, as you note, is designed for synchronous code, not async