2017-05-10 16 views
1

codefights.comからページを読み込み、特定のhtml要素が読み込まれるまで待っているときに "未処理の約束を却下"しています。ここでNightmare.jsで未処理の約束を拒否

は私のコードです:ここでは

import * as Nightmare from 'nightmare'; 

const nightmare = Nightmare({ show: true }); 

nightmare 
    .goto('https://codefights.com/interview/EDaACHNYHyH6qQFAL') 
    .wait('body > div:nth-child(9) > div > div.page--header > div > span') 
    .evaluate((selector) => { 
     return document.querySelector(selector); 
    }, 'body > div:nth-child(9) > div > div.page--header > div > span') 
    .end() 
    .then((functionTitle) => { 
     console.log(functionTitle); 
    }); 

は例外です:

Unhandled promise rejection (rejection id: 1): Error: Evaluation timed out after 30000msec. Are you calling done() or resolving your promises?

この問題を解決する方法上の任意のアイデアは?拒絶反応を処理するには

+0

あなたは未処理であることからoccuringからのタイムアウトや拒絶反応を防ぐためにしようとしていますか? – Bergi

+0

@Bergi私は、拒否が未処理であることを防止しようとしています。私もそれを理解したと思います。評価関数は 'then'の後に約束を返さなければなりません。 – Kiril

+0

そして、 '.catch(ハンドラー)'をチェーンに連結するか、ハンドラーを最後の 'チェーン'の第2引数として渡します。 – Bergi

答えて

0

は、ちょうどあなたのチェーンに.catch(handler)を鎖または最終chainに2番目の引数としてハンドラを渡す:

nightmare 
.goto('https://codefights.com/interview/EDaACHNYHyH6qQFAL') 
.wait('body > div:nth-child(9) > div > div.page--header > div > span') 
.evaluate(selector => { 
    return document.querySelector(selector); 
}, 'body > div:nth-child(9) > div > div.page--header > div > span') 
.end() 
.then(functionTitle => { 
    console.log(functionTitle); 
}, error => { 
    console.error(error); 
}); 
0

the documentation a bit moreを掘り後、私はevaluatePromiseを返すべきであることが判明しましたそれに続いてthenが続きます。

Promiseevaluateの一部としてサポートされています。関数の戻り値 にメンバーがある場合、.evaluate()は約束を待っていると仮定して です。ここで

それは固定だとき、それはどのように見えるかです:

nightmare 
.goto(url) 
.wait('body > div:nth-child(9) > div > div.page--header > div > span') 
.evaluate((selector) => { 
    return new Promise((resolve, reject) => { 
     try { 
      resolve(document.querySelector(selector).innerText); 
     } catch (exception) { 
      reject(exception); 
     } 
    }); 
}, 'body > div:nth-child(9) > div > div.page--header > div > span') 
.end() 
.then((functionTitle) => { 
    console.log(functionTitle); 
}); 
関連する問題