$ .whenall与Ajax

时间:2017-06-22 18:47:04

标签: javascript jquery ajax promise

我有一个函数来返回一个由AJAX-Call检索的feed,现在想要在完成一些异步请求之后做一些事情。

doit()是我最初调用的函数。

我很抱歉没有提供网址,但它是内部服务器。

这是我的代码:

   function grabFollowedCommunityPageFeed(page, cCallback) {
                $.ajax({
                    url: "blabla.com&page=" + page,
                    method: "GET",
                    contentType: "application/atom+xml"
                }).always(function(xhr, ignore, thrownMessage) {
                    var totalResults = 0;
                    if ((200 === thrownMessage.status) && (xhr)) {
                        totalResults = parseInt($(xhr).find("totalResults").first().text()) || -1;
                    }
                    if (cCallback && $.isFunction(cCallback)) {
                        cCallback({feed: xhr, resultCount: totalResults});
                    }
                });
            }

            function grabFollowedCommunitiesFeeds(pagecount) {
                var i = 1,
                    deferredArr = [];
                for (i = 1; i < pagecount; i += 1) {
                    grabFollowedCommunityPageFeed(i, function callback(resultObj) {
                        deferredArr[i] = new $.Deferred();
                        deferredArr[i].resolve(resultObj);
                    });
                }
                return deferredArr;
            }


            function doit() {
                var allCommunityFeedObjects = [],
                    allCommunityFeedObjectsCount = 0,
                    deferredObj = [];
                (function initialReadFollowedCommunityFeedPages() {
                    grabFollowedCommunityPageFeed(1, function(requestObj) {
                        allCommunityFeedObjectsCount = requestObj.resultCount;
                        var tEntries = $(requestObj.communityFeed).find("entry"),
                            el$;
                        $.each(tEntries, function(ignore, el) {
                            el$ = $(el);
                            if (!($.inArray(el$, allCommunityFeedObjects) !== -1)) {
                                allCommunityFeedObjects.push(el$);
                            }
                        });

                        deferredObj = grabFollowedCommunitiesFeeds(allCommunityFeedObjectsCount) || [];
                        $.whenAll.apply($, deferredObj).always(function(allCommunityFeeds) {
                            var k = allCommunityFeeds;
                            // union k with allCommunityFeedObjects
                        });
                    });
                })();

            }

这条线似乎也很好,我已经检查过了:

deferredArr[i].resolve(resultObj);

问题是

中的allCommunityFeeds参数未定义
  $.whenAll.apply($, deferredObj).always(function(allCommunityFeeds) 

这意味着出现了问题。你能救我吗?

0 个答案:

没有答案