2017-02-14 10 views
0

これはJavaScriptの問題ですが、これは、分度器テストの使用のために実装しようとしているものです。それは、関数呼び出しから戻った後、変数currentPremiumを使用しようと、それは常に未定義です分度器関数は未定義に戻りますか?

//fileA.js 
element(by.id('page-element').getText().then(function() { 
    var currentPremium = fileB.getSixMonthPremium(); // calls the function in fileB.js 

    element(by.id('page-element').getText().then(function() { 
     console.log(currentPremium); // prints undefined 
     fileB.compareValue(currentPremium, ..., ...,); 
    }); 
}); 


//fileB.js 
this.getSixMonthPremium() = function() { 
    element(by.id('full-premium').isDisplayed().then(function(displayed) { 
     if (displayed) { 
      element(by.id('full-premium').getText().then(function(currentPremium) { 
       console.log('Current Premium - ' + currentPremium); // prints string of $XXX.xx 
       return currentPremium; //seems to be returning undefined? 
      }); 
     } 
    }); 
}); 

。私は間違って何をしていますか?

答えて

1

Javascriptで非同期呼び出しを使用してようこそ!

getSixMonthPremium()コールから約束を返し、そのコールが戻ってから作業を続行したいと考えています。

fileB.getSixMonthPremium().then(function(premium){ 
    ...handle premium 
}); 
+0

ありがとう:

this.getSixMonthPremium() = function() { return new Promise(function(resolve,reject){ element(by.id('full-premium').isDisplayed().then(function(displayed) { if (displayed) { element(by.id('full-premium').getText().then(function(currentPremium) { console.log('Current Premium - ' + currentPremium); // prints string of $XXX.xx resolve(currentPremium); //seems to be returning undefined? }); } }); }) }); 

あなたは、以下のような何かをすることによってその約束を処理します!私はそれが何か非同期/約束に関係していることは分かっていましたが、私はGoogle検索で必要なものを見つけることができませんでした。私は自分自身でJavaScriptの本を入手するか、または良いオンラインのものを見つける必要があります:) – DrZoo

関連する問題