2017-08-09 5 views
2

このコードをテストして結果がアサートされるまで待ちます。問題がどこにあるかわからない場合は、最後にPromise.resolve()を返しますが、コードが実行される前にendを記録してください。async/Chromeリモートインターフェイスの問題を待ちます

Page.loadEventFiredの前にawaitを付ける必要がありますか?

const CDP = require('chrome-remote-interface') 

async function x() { 
    const protocol = await CDP() 

    const timeout = ms => new Promise(resolve => setTimeout(resolve, ms)) 

    // See API docs: https://chromedevtools.github.io/devtools-protocol/ 
    const { Page, Runtime, DOM } = protocol 
    await Promise.all([Page.enable(), Runtime.enable(), DOM.enable()]) 

    Page.navigate({ url: 'http://example.com' }) 

    // wait until the page says it's loaded... 
    return Page.loadEventFired(async() => { 
    console.log('Page loaded! Now waiting a few seconds for all the JS to load...') 
    await timeout(3000) // give the JS some time to load 

    protocol.close() 

    console.log('Processing page source...') 

    console.log('Doing some fancy stuff here ...') 

    console.log('All done.') 
    return Promise.resolve() 
    }) 
} 

(async function() { 
    console.log('start') 
    await x() 
    console.log('end') 
})() 

答えて

2

はい、ところで、あなたも常に近くプロトコルにtry-finallyでコードをラップすることがありますPage.loadEventFiredExample

async function x() { 
    const protocol = await CDP() 

    const timeout = ms => new Promise(resolve => setTimeout(resolve, ms)) 

    // See API docs: https://chromedevtools.github.io/devtools-protocol/ 
    const { Page, Runtime, DOM } = protocol 
    await Promise.all([Page.enable(), Runtime.enable(), DOM.enable()]) 

    await Page.navigate({ url: 'http://example.com' }) 

    // wait until the page says it's loaded... 
    await Page.loadEventFired() 

    console.log('Page loaded! Now waiting a few seconds for all the JS to load...') 

    await timeout(3000) // give the JS some time to load 

    protocol.close() 

    console.log('Processing page source...') 

    console.log('Doing some fancy stuff here ...') 

    console.log('All done.') 

} 

のために待つ必要があります。

async function x() { 
    let protocol 

    try { 
    protocol = await CDP() 
    ... 

    } finally { 
    if(protocol) protocol.close() 
    } 
+0

これは簡単でした。ありがとう! async/awaitを完全に取得しようとするともう少し時間がかかるでしょう。 – Patrick

関連する問題