试图将承诺转换为发电机

时间:2016-11-18 13:41:04

标签: javascript

我试图将我的承诺函数转换为生成函数,但我正在努力实现这一目标。

我在控制台日志中继续这样做:

var myGen {[[GeneratorStatus]]: "suspended"}

当我把它写成承诺时,它看起来像这样:

function maxYvalue2() {

     return Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).$promise.then(function(response){
        var maxYvalue = 0;
        var currMaxYvalue = 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;
            };
        }
        return maxYvalue;
    });

};


maxYvalue2().then(function(maxYvalue) {  


    var mxY = maxYvalue 

    console.log('var', 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, mxY], // <============ I then set mxY here in the $scope object
                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'
                }
            }
        };

    //console.log($scope.data_scn_cst);
}); 

然后我尝试重写为这样的生成器:

var myGen = function*() {

    var maxYvalue = 0;
    var currMaxYvalue = 0;

    var response = yield Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}});

    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;
        };
    }

};

var mxY = myGen();

console.log(mxY.next(1));



console.log('var', 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, mxY], // <============ I then set mxY here in the $scope object
            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'
            }
        }
    };

1 个答案:

答案 0 :(得分:1)

发电机不是承诺的替代品。

如果您想简化语法并避免then调用,请使用async / await(可能使用转换器)。当然,执行仍然是异步的,没有办法改变它。你会写

async function maxYvalue2() {
    var response = await Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).$promise;
//                 ^^^^^
    var maxYvalue = 0;
    var currMaxYvalue = 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;
        };
    }
    return maxYvalue;
}

这与您当前拥有的function maxYvalue2完全相同,但它仍然会返回一个承诺,因此您将以完全相同的方式调用它。