简体   繁体   English

在jQuery / JavaScript中使用循环和承诺在另一个函数之后运行一个函数

[英]Running a function after another function with loops & promises in jQuery/JavaScript

I'm working on a college/personal project which takes RSS urls and sends them through a series of APIs. 我正在一个大学/个人项目中,该项目需要RSS网址并通过一系列API发送它们。 Code follows: 代码如下:

var tempStory = [];
function getFeed () {
        $.getJSON('spoonfed/app/scripts/categories.json', function(categories){
            for (var category in categories){
                if (categories[category][2] === true){
                    getFeedURLs(categories[category][0]).then(function(rssData) {
                        for (var item in rssData){
                            tempStory.push(rssData[item]);
                        }
                    });
                }
            }
    });
}

There are an unknown number of categories I need to iterate over in addition to 10 news stories in each category, which are then pushed to the array tempStory. 除了每个类别中的10个新闻故事外,我还需要遍历多个类别,然后将这些新闻推送到tempStory数组中。 I need to run another function on this array of data. 我需要在此数据数组上运行另一个函数。 I think promises are the key here but I can't get my head around how I'd use them in this case. 我认为诺言是关键,但我无法理解在这种情况下如何使用诺言。

Any help would be really appreciated and I'm happy to provide more of the code if needed, including the structure of the categories.json file and the getFeedURLs function. 我们将不胜感激任何帮助,如果需要,我很乐意提供更多代码,包括category.json文件的结构和getFeedURLs函数。

If the issue is that you're just trying to figure out how to get all the data out, then you can do this: 如果问题是您只是想弄清楚如何获取所有数据,则可以执行以下操作:

function getFeed() {
    return $.getJSON('spoonfed/app/scripts/categories.json').then(function (categories) {
        var tempStory = [], promises = [];
        for (var category in categories) {
            if (categories[category][2] === true) {
                promises.push(getFeedURLs(categories[category][0]).then(function (rssData) {
                    for (var item in rssData) {
                        tempStory.push(rssData[item]);
                    }
                }));
            }
        }
        return $.when.apply($, promises).then(function() {
            // return results array as the fulfilled value of the promise
            return tempStory;
        });
    });
}

getFeed().then(function(data) {
    // all the data available here
}, function(err) {
    // error here
});

The key aspects of this are: 关键方面是:

  1. Return the original ajax promise from getFeed() . getFeed()返回原始的ajax许诺。
  2. When iterating the categories, push all those promises into an array to collect them all 遍历类别时,将所有这些诺言放入数组中以收集所有诺言
  3. Use $.when() with your array of promises to know when all of those are done $.when()与您的promise数组一起使用以知道何时完成所有这些操作
  4. From within the $.getJSON.then() handler, return the $.when() promise which will chain to the $.getJSON() promise and let you return an array of results. 从内$.getJSON.then()处理程序,返回$.when()承诺将链到$.getJSON()的承诺,让你返回结果的数组。
  5. From within the $.when().then() handler, return our results so that becomes the fulfilled value of the final promise $.when().then()处理程序中,返回我们的结果,以便成为最终承诺的实现值

Note: because of the way you chose to accumulate the tempStory array, it's results are not in a guaranteed order. 注意:由于您选择了累加tempStory数组的方式,因此其结果并非有保证的顺序。 Additional code could be written to maintain the order. 可以编写其他代码来维护顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM