docs: update console event snippet (#9800)

This avoids the confusion where arguments are printed
asynchronously, interleaved with other console messages.
This commit is contained in:
Dmitry Gozman 2021-10-26 14:40:28 -07:00 committed by GitHub
parent 22e4a0d580
commit fac5da9517
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 9 deletions

View File

@ -169,8 +169,10 @@ An example of handling `console` event:
```js
page.on('console', async msg => {
for (let i = 0; i < msg.args().length; ++i)
console.log(`${i}: ${await msg.args()[i].jsonValue()}`);
const values = [];
for (const arg of msg.args())
values.push(await arg.jsonValue());
console.log(...values);
});
await page.evaluate(() => console.log('hello', 5, {foo: 'bar'}));
```
@ -185,8 +187,10 @@ page.evaluate("() => console.log('hello', 5, {foo: 'bar'})");
```python async
async def print_args(msg):
values = []
for arg in msg.args:
print(await arg.json_value())
values.append(await arg.json_value())
print(values)
page.on("console", print_args)
await page.evaluate("console.log('hello', 5, {foo: 'bar'})")

View File

@ -794,8 +794,10 @@ export interface Page {
*
* ```js
* page.on('console', async msg => {
* for (let i = 0; i < msg.args().length; ++i)
* console.log(`${i}: ${await msg.args()[i].jsonValue()}`);
* const values = [];
* for (const arg of msg.args())
* values.push(await arg.jsonValue());
* console.log(...values);
* });
* await page.evaluate(() => console.log('hello', 5, {foo: 'bar'}));
* ```
@ -1065,8 +1067,10 @@ export interface Page {
*
* ```js
* page.on('console', async msg => {
* for (let i = 0; i < msg.args().length; ++i)
* console.log(`${i}: ${await msg.args()[i].jsonValue()}`);
* const values = [];
* for (const arg of msg.args())
* values.push(await arg.jsonValue());
* console.log(...values);
* });
* await page.evaluate(() => console.log('hello', 5, {foo: 'bar'}));
* ```
@ -3394,8 +3398,10 @@ export interface Page {
*
* ```js
* page.on('console', async msg => {
* for (let i = 0; i < msg.args().length; ++i)
* console.log(`${i}: ${await msg.args()[i].jsonValue()}`);
* const values = [];
* for (const arg of msg.args())
* values.push(await arg.jsonValue());
* console.log(...values);
* });
* await page.evaluate(() => console.log('hello', 5, {foo: 'bar'}));
* ```