2017-12-28 30 views
0

分度器を使用してテストしたいキャンバス円グラフがあります。分度器を使って角度js変数を評価する

... 
var canvas = element(by.css("canvas#my_pie")); 
... 
canvas.evaluate("ctl.gfx.categories").then(function(c){ 
     console.log(c);    
    }); 
canvas.evaluate("ctl.gfx.values").then(function(l){ 
     console.log(v); 
    }); 

すべてが正常に動作し、データがログアウトされます。私は次のように各属性データ値およびデータ・カテゴリーを評価することができる午前私の分度器コードで

<canvas id ="my_pie" data-values="ctl.gfx.values" data-categories="ctl.gfx.categories"... 

:よう html要素に見えますコンソール上では配列を返そうとすると空の配列として返されますが、これは分度器の評価関数の意味での約束とは関係がありますが、完全に新しいJS、ジャスミンと分度器に。

基本的に期待されるロジックは次のようにする必要があります:私がやりたいものを

function getMyData(){ 
    var myCategories = []; 
    var myValues = []; 

    canvas.evaluate("ctl.gfx.categories").then(function(c){ 
     myCategories = c;    
    }); 

    canvas.evaluate("ctl.gfx.values").then(function(v){ 
     myValues = v; 
    }); 

    // do something with both arrays, concat and return a result 
    //final statement would be a return statement. 
} 

は、したがって、いくつかの処理の後に、これらの配列の結果を返しますが、return文は、最初に必ず実行されるように私にそれはそうです空の結果。あなたは分度器で非同期プログラミング/約束を理解していない

答えて

0

、表現するために何を学び、あなたがキーポイントにこのリンクをオフにしてください:http://www.protractortest.org/#/control-flow#promises-and-the-control-flow

その後、私は時に実行が起こるかを説明するために、他の質問に答えるこのリンクを調べますProtracotrスクリプト:promise chaining vs promise.all

は、その後、私はあなたが常に

function getMyData(){ 
    var myCategories = []; 
    var myValues = []; 

    canvas.evaluate("ctl.gfx.categories").then(function(c){ 
     myCategories = c;    
    }); 

    canvas.evaluate("ctl.gfx.values").then(function(v){ 
     myValues = v; 
    }); 

    // Assume we concat the two array and return the new array as below code shows 
    var ret = myCategories.concat(myValues); 
    return ret; 

    // When JavaScript engine explain and execute below code line: 
    // var ret = myCategories.concat(myValues); 

    // the above two canvas.evaluate() have not resoved, because they are 
    // executed async not sync. 
    // so the myCategories and myValues have not assigned new value, they are 
    // still empty array at this time point, so always return empty array 
} 

あなたの問題を解決するには、コードのコメントに空の配列を取得する理由は、コード例を下にしてみてください説明し、下記をご覧:

function getMyData(){ 

    return Promise.all([ 
     canvas.evaluate("ctl.gfx.categories"), 
     canvas.evaluate("ctl.gfx.values") 
    ]).then(function(data){ 
     let myCategories = data[0]; 
     let myValues = data[1]; 
     return myCategories.concat(myValues); 
    }); 
} 


getMyData().then(function(myData){ 
    console.log(myData); 
    // put your code to consume myData at here 
}) 
+0

ありがとうございました。 –

関連する問題