haxiomic / dts2hx

Converts TypeScript definition files (d.ts) to haxe externs (.hx) via the TypeScript compiler API
MIT License
136 stars 9 forks source link

support puppeteer? #52

Closed sonygod closed 4 years ago

sonygod commented 4 years ago

support puppeteer?

there is some async don't know how to write in haxe.

haxiomic commented 4 years ago

Can you give an example? I'm not sure what you want to do

Usually async code is mapped to js promises so you can normally interact with it without the async await keywords

sonygod commented 4 years ago

const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage();

await page.setViewport({ width: 1920, height: 1080 }); await page.goto('https://www.qikegu.com'); await page.waitForSelector('title');

// 截屏页面中的一个区域 await page.screenshot({ path: 'screenshot.jpg', type: 'jpeg', quality: 80, clip: { x: 0, y: 0, width: 300, height: 300 } });

await browser.close();})();

On Sat, 26 Sep 2020, 19:33 George Corney, notifications@github.com wrote:

Can you give an example? I'm not sure what you want to do

Usually async code is mapped to js promises so you can normally interact with it without the async await keywords

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/haxiomic/dts2hx/issues/52#issuecomment-699483484, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFWWIBW2DO3WBH7KFL5TL3SHXGSLANCNFSM4R2Y4XCA .

haxiomic commented 4 years ago

Haxe native async features are still in development, however in the meantime you can use libraries like https://github.com/haxetink/tink_await to add async/await keywords

If you don't want to use libraries then since the await methods return promises, you can do:

puppeteer.launch().then(browser -> {
    browser.newPage().then(page -> {
        // rest of code
    });
});

or without nested callbacks

Promise.all([puppeteer.launch(), browser.newPage()]).then(array -> {
    var browser = array[0];
    var page = array[1];
});
sonygod commented 4 years ago
   package;

import Puppeteer;
import puppeteer.devices.*;
import puppeteer.Browser;
using tink.CoreApi;

@await
class Main {
    @async
    public static function main() {
        var browser:Browser = @await Puppeteer.launch({headless: false, devtools: true}).toPromise();

        var page=@await browser.newPage().toPromise();

    }
}