2016-11-17 12 views
1

私は、変数mxYに設定しようとしている約束を返す関数を持っています(JavaScriptが初めてだと思います)。私はこのように私の関数を書いている:外部変数への約束を設定する

function maxYvalue2() { 
    return Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).$promise.then(function(response){ 
     var maxYvalue = 0 
     for (var i=0;i<response.length;i++) { 
      var currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost); 
      if (currMaxYvalue > maxYvalue) { 
       maxYvalue = currMaxYvalue 
      }; 
     } 
     console.log("yVal: " + maxYvalue) 
     return maxYvalue; 
    }); 
}; 

var mxY = maxYvalue2().then(function (response) { 
    console.log("fnc: ", response); 
    return response; 
}); 


console.log(mxY); 

と私のコンソールがmxY{$$state: Object}をログに記録するようだところ、この、示しています。私はこれまで理解して何から

reports.controller.js:99 d {$$state: Object} 
    $$state: Object 
     status: 1 
     value: 78820.3574413 
     __proto__: Object 
     __proto__: Object 
reports.controller.js:88 yVal: 78820.3574413 
reports.controller.js:94 fnc: 78820.3574413 

を、maxYvalue2().thenが戻っています私が戻って$$state.Objectを返す理由ですが、私がする必要があるのは、それが解決するときに約束を「解き放つ」ことです、そうか、私はここから完全に離れていますか?そしてそれを変数にセットしましたか?

------ EDIT ---------

私は以下の強調表示された領域にyDomain: [0, mxY]yDomain: [0, 100000]を変更...ここに行くためにmaxYvalue2()の結果を取得しようとしています:

function maxYvalue2() { 
    return Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).$promise.then(function(response){ 
     var maxYvalue = 0 
     for (var i=0;i<response.length;i++) { 
      var currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost); 
      if (currMaxYvalue > maxYvalue) { 
       maxYvalue = currMaxYvalue 
      }; 
     } 
     console.log("yVal: " + maxYvalue) 
     return maxYvalue; 
    }); 
}; 

var mxY = maxYvalue2().then(function (response) { 
    console.log("fnc: ", response); 
    return response; 
}); 


console.log(mxY); 


    $scope.options_scn_cst = { 
      chart: { 
       type: 'lineChart', 
       height: 450, 
       margin : { 
        top: 20, 
        right: 20, 
        bottom: 40, 
        left: 55 
       }, 
       x: function(d){ return d.x; }, 
       y: function(d){ return d.y; }, 
       useInteractiveGuideline: true, 
       dispatch: { 
        stateChange: function(e){ console.log("stateChange"); }, 
        changeState: function(e){ console.log("changeState"); }, 
        tooltipShow: function(e){ console.log("tooltipShow"); }, 
        tooltipHide: function(e){ console.log("tooltipHide"); } 
       }, 
       xAxis: { 
        axisLabel: '', 
        tickFormat: function(d) { return d3.time.format('%b %y')(new Date(d)); } 
       }, 
       yDomain: [0, 100000], //<======change the 100000 to the var mxY 
       yAxis: { 
        axisLabel: '$/month', 
        tickFormat: function(d){ 
         return d3.format('$,.0f')(d); 
        }, 
        axisLabelDistance: -10 
       }, 
       callback: function(chart){} 
      }, 
      title: { 
       enable: true, 
       text: 'Scenario Costs Over Time' 
      }, 
      subtitle: { 
       enable: false, 
       text: 'Put your Subtitle here.', 
       css: { 
        'text-align': 'center', 
        'margin': '10px 13px 0px 7px' 
       } 
      }, 
      caption: { 
       enable: false, 
       html: 'Put your Caption Here.', 
       css: { 
        'text-align': 'justify', 
        'margin': '10px 13px 0px 7px' 
       } 
      } 
     }; 
+0

「アンラップ」とはどういう意味ですか?あなたの約束事とconsole.logは期待値を示しているようです。 – tsuz

+0

'then'コールバックで値を必要とするすべてのコードを移動します。それは、それを「アンラップする」唯一の方法です。 – Bergi

+0

ok ... 'then()'の中に '$ scope.options_scn_cst'を移動しましたが、今は' $ scope.options_scn_cst'にアクセスできないようです... – user2061886

答えて

1

約束があると、それを同期的にアンラップする方法はありません。非同期プログラミングの世界へようこそ!

then()句(またはエラーがある場合はcatch()句)の中でのみ、約束の解決を取得できます。

console.log(mxY);に電話すると、約束を作成したのと同じティックで約束自体にアクセスしています。約束はまだ解決できませんでした。 JavaScriptはシングルスレッドなので、非同期以外の約束の解決には決してアクセスできません。

したがって、はい、then()句を使用する必要があります。そしてありがたいことに、約束は、thenをチェーンするメカニズムを提供します。

あなたはこれを行うことができます:これが表示されている何

maxYvalue2().then(function (response) { 
    console.log("fnc: ", response); 
    return response; 
}).then(function (response) { 
    // do something 
    return newResponse 
}).then(function (newResponse) { 
    // do something 
    return notherNewResponse 
}).then(function (notherNewResponse) { 
    // etc 
    return notherNotherNewResponse 
}); 

を、あなたの約束をチェーンし、いくつかのかなり複雑な非同期計算を実行するためにそれらを使用することができるということです。

+0

例 'var foo = response'は' then() 'の中にありますか?私が混乱しているところは、 'foo'が' then() 'の中に設定されている場合、' maxYvalue2'の外で 'foo'をどのように呼び出すのでしょうか? – user2061886

+1

答えはできないということです。値は非同期に到着します。コールバックと約束の連鎖を通じて価値にアクセスする必要があります。 –

+0

ok ...そういうわけで、私は、 'then()'の内部に外部の 'var'を使っているという論理を置く必要があるのですか?私はコードに 'maxYvalue2'の結果をどこに入れたいかを示すコードをいくつか追加しました。申し訳ありませんが、これらはかなり基本的な質問です... – user2061886

関連する問題