rialto-php / rialto

Manage Node resources with PHP
MIT License
170 stars 80 forks source link

Support async/await syntax in JS functions #5

Closed nesk closed 6 years ago

nesk commented 6 years ago

Currently, it is impossible to use await in a JS function because the function isn't defined as async. Making all the JS functions async by default is not a good option because some Puppeteer's events could one day require a return value.

A good option would be to add a chaining method to return an async JS function:

JsFunction::create("console.log(await 'Hello world!')")->async()

The above function creation would generate the following JS code:

return async function () {
    var page = global.puphpeteerResources['a-unique-id-to-the-resource'];
    console.log(await page.content())
};

Important: the documentation must be updated.


Method chaining should probably be implemented before dealing with this issue.

JoelEinbinder commented 6 years ago

It is extremely unlikely that puppeteer will require a return value for events. For the same reason as you, we wouldn’t want marking functions as async to drastically change the behavior.

nesk commented 6 years ago

Oh, that's some good news! However, I think I will stick with this approach since its closer to the way JS works (the fact that functions are sync by default).

Thank you for the info!