2017-12-28 7 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. 
} 

는 내가 뭘하고 싶은 것은 일부 처리 후 그 배열의 결과를 반환하지만 반환 문을 따라서 먼저 항상 실행처럼 내게는 보인다처럼

는 기본적으로 예상 논리는해야한다 빈 결과.

답변

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 
} 

문제를 해결하려면 코드 주석의 빈 상태 (empty)의 배열이 왜 코드 예제 아래 시도 설명, 아래에 보이는 :

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

도움 주셔서 감사합니다. –

관련 문제