忽略与承诺阵列和$。当AJAX错误 [英] Ignoring AJAX errors with promises array and $.when

查看:102
本文介绍了忽略与承诺阵列和$。当AJAX错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,获取的JSON来自YouTube的视频ID的数组。它的工作原理伟大的,当所有的视频​​存在,查询成功。它发送一些的getJSON请求,当所有的人都做了......在 $。when.done()火灾,我可以处理结果数据。

I have the following code that gets the JSON from an array of YouTube video ids. It works great when all the videos exists and the query is successful. It sends several getJSON request, and when all of them are done... the $.when.done() fires and I can process the resulting data.

var
  results  = {},
  promises = [];

$(document).ready(function() {

  var
    vids = [
    'ozj2-bnTL3s',
    'EAZ4Tlt8MQ4',
    'Xn9o7cxqVoA'
    // ,'this-videoid-doesnot-exists'
  ],
  url = 'http://gdata.youtube.com/feeds/api/videos/{{vid}}?v=2&alt=json';

  $.each(vids, function(idx, vid){
    var
      u = url.replace('{{vid}}', vids[idx]),
      p = null;

    p = $.getJSON( u ).done(function(data) {
      results[vid] = data.entry;
    });

    promises.push(p);
  });

  $.when.apply($, promises).done(function(){
    console.log(results);
  });

});

不过......在最后的应用程序,如果所有的视频​​仍存在的YouTube,我已经意识到,有时一个(或几个)的列表中的视频可能已被删除......或者我不控制我从DB获得ID不正确。

But... In the final app I don't control if all the videos still exists in YouTube, I've realized that sometimes one (or several) of the videos in the list may have been deleted... or the id that I get from the DB is incorrect.

有什么办法,我可以放心地添加到结果变量只有视频,凡成功白化触发$ .when.fail()?并等待所有的查询已经完成..

Is there any way that I could safely add to the results variable only the videos that where successful whiteout trigger the $.when.fail()? and waiting that all the queries had finished...

我的意思是,我的最终目标是获得存在的视频数据(那些其数据被成功取出),而且,在某种程度上忽略那些不存在或者无法使用......而且我不W图,现在该怎么办呢。

I mean, my final goal is to get the data from the videos that exist (those that its data was successfully retrieved), and, somehow to ignore those that don't exist or where unavailable... and I don't figure right now how to do it.

任何想法/方法将AP pretiated。 TIA!
您可以找到code在此的jsfiddle

Any idea / approach will be appretiated. TIA!
You can find the code in this JSFiddle

推荐答案

不幸的是,jQuery的不来使用这一功能。

Unfortunately, jQuery doesn't come with this functionality.

欣然,你可以很容易地实现它自己pretty的。

Gladly, you can implement it yourself pretty easily.

var url = 'http://gdata.youtube.com/feeds/api/videos/{{vid}}?v=2&alt=json';
function getVideo(vid){
    var u = url.replace('{{vid}}', vid);
    return $.getJSON( u ).then(function(res){
         return {video:vid,result:res.entry};
    });
}
var promises = ['ozj2-bnTL3s','EAZ4Tlt8MQ4',"doesn'texist"].map(getVideo);

some(promises).then(function(results){
    for(var i = 0; i < results.length; i++) {
        console.log(results[i]); // log
    }
});



// get a hook on when all of the promises resolve, some fulfill
// this is reusable, you can use this whenever you need to hook on some promises
// fulfilling but all resolving.
function some(promises){
    var d = $.Deferred(), results = [];
    var remaining = promises.length;
    for(var i = 0; i < promises.length; i++){
        promises[i].then(function(res){
            results.push(res); // on success, add to results
        }).always(function(res){
            remaining--; // always mark as finished
            if(!remaining) d.resolve(results);
        })
    }
    return d.promise(); // return a promise on the remaining values
}

下面是一个工作结果的jsfiddle

这篇关于忽略与承诺阵列和$。当AJAX错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆