async function inside evaluate fails
See original GitHub issueCode
await page.evaluate(async () => {
console.log('1')
})
Error
(node:66796) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: ReferenceError: fn is not defined
at <anonymous>:1:26
Issue Analytics
- State:
- Created 6 years ago
- Reactions:15
- Comments:16 (6 by maintainers)
Top Results From Across the Web
Puppeteer - Async function in evaluate method throws error
If I want to call async method in evaluate function, I get Error: Evaluation failed: [object Object] error. const puppeteer = require(" ...
Read more >async inside evaluate function is not working #3312 - GitHub
I get the error below when I run this code, I have no idea why its not working. await page.evaluate(async () => {...
Read more >await - JavaScript - MDN Web Docs
The await operator is used to wait for a Promise and get its fulfillment value. It can only be used inside an async...
Read more >Dart: using `await` when evaluating async function results in ...
Dart: using `await` when evaluating async function results in compilation error ; Project, WebStorm ; Type, Bug ; State, Incomplete I ; Assignee,...
Read more >Puppeteer Evaluation failed: ReferenceError: __awaiter is ...
So, to make the TypeScript compiler not transform async functions, you need to change the compilerOptions.target in tsconfig.json to at least ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Are you transforming your code with babel? The babel async function code isn’t compatible with Puppeteer. We call
function.toString
and send your code into Chromium, but babel messes with that and we end up sending an incomplete string. You can get around this by using template strings instead of functions.same issue, different error, using TypeScript (not babel). The solution for me was to instruct TypeScript to compile to latest ecma version so no pollyfills / artificial code is generated for async function support, in tsconfig.json file:
issue details
While the following works OK:
the equivalent code using async functions throws the following error:
Error: