Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery承诺-即使未找到某些json,也会启动_Javascript_Jquery_Promise - Fatal编程技术网

Javascript jQuery承诺-即使未找到某些json,也会启动

Javascript jQuery承诺-即使未找到某些json,也会启动,javascript,jquery,promise,Javascript,Jquery,Promise,我使用的是promise,但有时我无法从各种原因获得JSON。在一种情况下,即使缺少一些JSON,我如何启动它呢 $.when( arrayResults[0] ? $.getJSON("url") : null, arrayResults[1] ? $.getJSON("url") : null, arrayResults[2] ? $.getJSON("url") : null ).done(function () { }).fail(function () {

我使用的是promise,但有时我无法从各种原因获得JSON。在一种情况下,即使缺少一些JSON,我如何启动它呢

$.when(
    arrayResults[0] ? $.getJSON("url") : null,
    arrayResults[1] ? $.getJSON("url") : null,
    arrayResults[2] ? $.getJSON("url") : null

).done(function () { }).fail(function () {
    console.log('Failed');
});

您可以使用延迟。始终(cb):


请参阅此处的进一步信息:

如果您使用的是jQueryV3+它是兼容的,因此您可以将
catch()
添加到请求承诺中

无论何时从catch中返回,它都会解析先前的承诺,并将返回的内容传递给承诺链中的下一个
then()

函数getData(url){ 返回$.getJSON(url) .然后(数据=>数据) .catch(解析失败) } 函数解析失败(jqXhr){ //返回你想要的任何东西。我添加了状态,以防感兴趣 //可以返回'false'或字符串或其他任何内容 //如果需要,还可以将任何问题记录回服务器 返回{ 错误:正确, 状态:jqXhr.status, statusText:jqXhr.statusText }; } var req=getData('https://api.myjson.com/bins/l9ywp'), req2=getData('https://httpbin.org/FAIL'), req3=getData('https://api.myjson.com/bins/l9ywp'); //也可以将“$.when”替换为“Promise.all()` $.when(请求、请求2、请求3)。然后(函数(r1、r2、r3){ //根据catch()中返回的内容验证参数 console.log('r1',r1); console.log('r2',r2);//从catch()返回的对象 console.log('r3',r3); });
我在函数中遇到问题,然后在'data'和=>之间,我得到了预期的错误表达式,在''之后,我得到了预期的错误表达式,预期的是新行或分号。同样,对于这段代码,我仍然无法启动。表单只是不加载。它总是在以下位置失败:last req4=getData(Url);将
.then(data=>data)
更改为
.then(function(data){returndata})
。第一个是ES6语法抱歉,如果我现在让你工作过度,但我已经筋疲力尽,大脑几乎无法工作,现在你的示例看起来不错,但在执行此操作时,我得到了“uncaughttypeerror:$.getJSON(…)。然后(…).catch不是函数”什么版本的jQuery?可能不是v3
$.when(
    arrayResults[0] ? $.getJSON("url") : null,
    arrayResults[1] ? $.getJSON("url") : null,
    arrayResults[2] ? $.getJSON("url") : null
)
.done(function () { console.log('I will run when the promise was resolved') })
.fail(function () { console.log('I will run when the promise was rejected') })
.always(function() { console.log('I will always fire, regardless of previous results') })