如何解决我的诺言回到这里这个功能呢? [英] How to fix my promise return here for this function?

查看:166
本文介绍了如何解决我的诺言回到这里这个功能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它最多可包含3项标记的数组。

接下来,我通过标记进入我的 getTagQuotes 功能,我试图承诺变量设置为它。 现在我得到的承诺是不确定的。

在这里输入的形象描述

  //如果有标签,等待在这里承诺:
如果(tags.length大于0){
    VAR承诺= getTagQuotes(标签)。然后(函数(){
        的console.log('承诺=',承诺);        对于(VAR I = 0; I< tweetArrayObjsContainer.length;我++){
            chartObj.chartData.push(tweetArrayObjsContainer [I]);
        }        chartDirective = ScopeFactory.getScope('表');
        chartDirective.nvd3.drawChart(chartObj.chartData);
    });
}
其他{
    chartDirective = ScopeFactory.getScope('表');
    chartDirective.nvd3.drawChart(chartObj.chartData);
}

^以上的目标是确保在 tweetArrayObjsContainer 数组已经排满,并​​被推到 chartObj.chartData 之前我画我的 nvd3

下面是我的 getTagQuotes 函数做另一个循环通过标签(最多3个),并调用其他服务 GetTweetVolFactory.returnTweetVol 这使得实际的API调用来检索数据。

我有code。如果我们在最后一步循环哪些检查,然后调用 formatTagData 功能。

formatTagData 是我填的是 tweetArrayObjsContainer

  //检查及获取标签数据:
////////////////////////////////////////////////// //////////////////
功能getTagQuotes(标签){
    的console.log('getTagQuotes调用,标签);
    变种推迟= $ q.defer(); //< - 设置$ q.defer这里
    VAR URL ='应用程序/ API /社交/微博/体积/';    对于(VAR I = 0; I< tags.length;我++){
        VAR loopStep = I;
        rawTagData = [];        GetTweetVolFactory.returnTweetVol(URL +标签[I] .term_id)
            .success(功能(数据,状态,头,配置){
                rawTagData.push(数据);                如果(loopStep ===(rawTagData.length - 1)){
                    // formatTagData填充数据的tweetArrayObjsContainer:
                    formatTagData(rawTagData);
                    deferred.resolve(); //< - 最后一步,所以解决
                    返回deferred.promise;
                }
            })
            .error(功能(数据,状态){
                回报在返回的鸣叫数据错误;
            });
    }    功能formatTagData(RAWDATA){
        tweetArrayObjsContainer = [];        对于(VAR I = 0; I< rawData.length;我++){
            VAR data_array中= [];
            VAR loopNum = I;            。_(RAWDATA [I] .frequency_counts).reverse()值();            对于(VAR J = 0; J< RAWDATA [loopNum] .frequency_counts.length; J ++){
                变种data_obj = {};
                RAWDATA [loopNum] .frequency_counts [J] .start_epoch = addZeroes(RAWDATA [loopNum] .frequency_counts [J] .start_epoch);
                data_obj.x = RAWDATA [loopNum] .frequency_counts [J] .start_epoch;
                data_obj.y = RAWDATA [loopNum] .frequency_counts [J] .tweets;
                data_array.push(data_obj);
            }            VAR tweetArrayObj = {
                钥匙:数量+第(i + 1),
                类型:区,
                Y轴:1,
                色:tagColorArray [I]
                价值观:将data_array
            };            tweetArrayObjsContainer.push(tweetArrayObj);
        }
    }
}


解决方案

getTagQuotes 函数返回的 $ Q 服务,检查出来

这应该是这样的:

 函数getTagQuotes(标签){
    变种推迟= $ q.defer();    (......)
    //当在这里完成的动作,通话时解决:
    deferred.resolve();
    (......)    //而在最后,返回的承诺
    返回deferred.promise;
}

最后,在主code,你会做的:

  VAR承诺= getTagQuotes(标签)。然后(函数(){
    的console.log('承诺=',承诺);
    //填写chartObj与鸣叫数据数组
    (......)
});

I have an array of tags which may contain up to 3 items.

Next I pass tags into my getTagQuotes function and am trying to set a promise variable to it. Right now I'm getting promise is undefined.

// If there are tags, the wait for promise here:
if (tags.length > 0) {
    var promise = getTagQuotes(tags).then(function() {
        console.log('promise =',promise);

        for (var i=0; i<tweetArrayObjsContainer.length; i++) {
            chartObj.chartData.push(tweetArrayObjsContainer[i]);
        }

        chartDirective = ScopeFactory.getScope('chart');
        chartDirective.nvd3.drawChart(chartObj.chartData);
    });
}
else {
    chartDirective = ScopeFactory.getScope('chart');
    chartDirective.nvd3.drawChart(chartObj.chartData);
}

^ The goal above is to make sure that arrays in tweetArrayObjsContainer have been filled and pushed into chartObj.chartData before I draw my nvd3 chart.

Below is my getTagQuotes function which does another loop through the tags (up to 3) and calls another service GetTweetVolFactory.returnTweetVol which makes the actual API call to retrieve data.

I have code which checks if the we're on the final loop step, then calls the formatTagData function.

Inside formatTagData is where I fill the tweetArrayObjsContainer.

// Check for and GET tag data:
////////////////////////////////////////////////////////////////////
function getTagQuotes(tags) {
    console.log('getTagQuotes called with',tags);
    var deferred   = $q.defer(); // <- setting $q.defer here
    var url        = 'app/api/social/twitter/volume/';

    for (var i=0; i<tags.length; i++) {
        var loopStep = i;
        rawTagData   = [];

        GetTweetVolFactory.returnTweetVol(url+tags[i].term_id)
            .success(function(data, status, headers, config) {
                rawTagData.push(data);

                if (loopStep === (rawTagData.length - 1)) {
                    // formatTagData fills the tweetArrayObjsContainer with data:
                    formatTagData(rawTagData);
                    deferred.resolve(); // <-- last step, so resolve
                    return deferred.promise;
                }
            })
            .error(function(data, status) {
                return 'error in returning tweet data';
            });
    }

    function formatTagData(rawData) {
        tweetArrayObjsContainer = [];

        for (var i=0; i<rawData.length; i++) {
            var data_array = [];
            var loopNum = i;

            _(rawData[i].frequency_counts).reverse().value();

            for (var j=0; j<rawData[loopNum].frequency_counts.length; j++) {
                var data_obj = {};
                rawData[loopNum].frequency_counts[j].start_epoch = addZeroes(rawData[loopNum].frequency_counts[j].start_epoch);
                data_obj.x = rawData[loopNum].frequency_counts[j].start_epoch;
                data_obj.y = rawData[loopNum].frequency_counts[j].tweets;
                data_array.push(data_obj);
            }

            var tweetArrayObj = {
                "key"    : "Quantity"+(i+1),
                "type"   : "area",
                "yAxis"  : 1,
                "color"  : tagColorArray[i],
                "values" : data_array
            };

            tweetArrayObjsContainer.push(tweetArrayObj);
        }
    }
}

解决方案

Your getTagQuotes function should return a promise with the $q service, check it out.

It should look like this:

function getTagQuotes(tags) {
    var deferred = $q.defer();

    (...)
    // When the action is finished here, call resolve:
    deferred.resolve();
    (...)

    // And in the end, return the promise
    return deferred.promise;
}

And finally, in your main code, you'll do:

var promise = getTagQuotes(tags).then(function(){
    console.log('promise =',promise);
    // Fill chartObj with tweet data arrays
    (...)
});

这篇关于如何解决我的诺言回到这里这个功能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆