如何修复我的承诺在这里返回此功能? [英] How to fix my promise return here for this function?

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

问题描述

我有一个 tags 数组,其中最多可包含 3 个项目.

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

接下来,我将 tags 传递到我的 getTagQuotes 函数中,并尝试为其设置一个 promise 变量.现在我得到的承诺是未定义的.

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

^ 上面的目标是确保在我绘制 nvd3 之前,tweetArrayObjsContainer 中的数组已被填充并推送到 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.

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

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.

我有代码检查我们是否处于最后的循环步骤,然后调用 formatTagData 函数.

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

formatTagData里面是我填充tweetArrayObjsContainer的地方.

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

推荐答案

你的 getTagQuotes 函数应该返回一个带有 $q 服务的承诺,检查一下.

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

它应该是这样的:

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天全站免登陆