如何返回后单诺循环(产生在每个迭代上的承诺)是完整的? [英] How to return single promise after for loop (which produces a promise on every iteration) is complete?
本文介绍了如何返回后单诺循环(产生在每个迭代上的承诺)是完整的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的承诺回报code的一个问题,我有一个函数 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.
我如何code这个开头如下:
How my code for this begins below:
// 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)
这是我的内循环:
it's inside my for loop:
for (var i=0; i<tags.length; i++)
一切都很正常,如果我只能通过一次有循环。但是只要有另一个标签(最多3个),它仍然只返回第一个循环/数据。它不会等到循环已经完成。然后返回的承诺。所以,我的 tweetArrayObjsContainer
总是只有第一个标记。
推荐答案
三个问题:
- 您没有回从
getTagQuotes
方法递延承诺。 - 您在看
I
来看看,如果你是通过循环和for循环已经完成(我==(标签。长度 - 1)
)之前的第一个成功甚至被称为 。 - 您叫
收益
在循环的第一次迭代,让你甚至没有得到第二个项目。
- 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.
下面是纠正code(没有测试它尚未)
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;
}
这篇关于如何返回后单诺循环(产生在每个迭代上的承诺)是完整的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文