如何在 for 循环(在每次迭代中产生一个承诺)完成后返回单个承诺? [英] How to return single promise after for loop (which produces a promise on every iteration) is complete?
问题描述
我的 promise 返回代码有问题,我有一个函数 getTagQuotes
,其中包含一个 for 循环,该循环可以多次调用 API 以将数据返回到数组中.
I have a problem with my promise return code, I have a function getTagQuotes
which contains a for loop which can make multiple calls an API to return data into an array.
我的代码如下:
// If there are tags, then wait for promise here:
if (tags.length > 0) {
// Setting promise var to getTagQuotes:
var promise = getTagQuotes(tags).then(function() {
console.log('promise =',promise);
// This array should contain 1-3 tags:
console.log('tweetArrayObjsContainer =',tweetArrayObjsContainer);
// Loop through to push array objects into chartObj:
for (var i=0; i<tweetArrayObjsContainer.length; i++) {
chartObj.chartData.push(tweetArrayObjsContainer[i]);
}
// Finally draw the chart:
chartDirective = ScopeFactory.getScope('chart');
chartDirective.nvd3.drawChart(chartObj.chartData);
});
}
我的 getTagQuotes 函数返回承诺:
My getTagQuotes function with the promise return:
function getTagQuotes(tags) {
var deferred = $q.defer(); // setting the defer
var url = 'app/api/social/twitter/volume/';
// My for loop, which only returns ONCE, even if there are 3 tags
for (var i=0; i<tags.length; i++) {
var loopStep = i;
rawTagData = [];
// The return statement
return GetTweetVolFactory.returnTweetVol(url+tags[i].term_id)
.success(function(data, status, headers, config) {
rawTagData.push(data);
// One the last loop, call formatTagData
// which fills the tweetArrayObjsContainer Array
if (loopStep === (rawTagData.length - 1)) {
formatTagData(rawTagData);
deferred.resolve();
return deferred.promise;
}
});
}
function formatTagData(rawData) {
for (var i=0; i<rawData.length; i++) {
var data_array = [];
var loopNum = i;
for (var j=0; j<rawData[loopNum].frequency_counts.length; j++) {
var data_obj = {};
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"+(loopNum+1), "type" : "area", "yAxis" : 1, "values" : data_array
};
tweetArrayObjsContainer.push(tweetArrayObj);
}
}
}
注意这一行
return GetTweetVolFactory.returnTweetVol(url+tags[i].term_id)
它在我的 for 循环中:
it's inside my for loop:
for (var i=0; i<tags.length; i++)
如果我只需要循环一次,一切都很好.然而,只要有另一个标签(最多 3 个),它仍然只返回第一个循环/数据.它不会等到 for 循环完成.然后返回承诺.所以我的 tweetArrayObjsContainer
总是只有第一个标签.
Everything works great if I only have to loop through once. However as soon as there is another tag (up to 3) it still only returns the first loop/data. It does not wait till the for loop is done. Then return the promise. So my tweetArrayObjsContainer
always only has the first tag.
推荐答案
三个问题:
- 您没有从
getTagQuotes
方法返回延迟承诺. - 你正在查看
i
是否通过循环,并且 for 循环已经完成 (i == (tags.length - 1)
)在调用第一个成功之前. - 您在循环的第一次迭代中调用了
return
,因此您甚至没有到达第二个项目.
- you didn't return the deferred promise from the
getTagQuotes
method. - you were looking at
i
to see if you were through the loop, and the for loop is already completed (i == (tags.length - 1)
) before the first success is even called. - you called
return
in the first iteration of the loop so that you didn't even get to the 2nd item.
这是更正后的代码(尚未测试)
Here's corrected code (didn't test it yet)
function getTagQuotes(tags) {
var deferred = $q.defer(); // setting the defer
var url = 'app/api/social/twitter/volume/';
var tagsComplete = 0;
for (var i=0; i<tags.length; i++) {
rawTagData = [];
GetTweetVolFactory.returnTweetVol(url+tags[i].term_id)
.success(function(data, status, headers, config) {
rawTagData.push(data);
tagsComplete++;
if (tagsComplete === tags.length) {
formatTagData(rawTagData);
deferred.resolve();
}
});
}
return deferred.promise;
}
这篇关于如何在 for 循环(在每次迭代中产生一个承诺)完成后返回单个承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!