Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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 如何确定所有ajax请求都已解决?_Javascript_Jquery_Ajax_Promise - Fatal编程技术网

Javascript 如何确定所有ajax请求都已解决?

Javascript 如何确定所有ajax请求都已解决?,javascript,jquery,ajax,promise,Javascript,Jquery,Ajax,Promise,这是我的密码: function ajaxRequest(value, path, website){ var status = false; return new Promise(function (resolve, reject) { window[website] = $.ajax({ url : path, type : 'GET', data: { "name": value,

这是我的密码:

function ajaxRequest(value, path, website){
    var status = false;
    return new Promise(function (resolve, reject) {
        window[website] = $.ajax({
            url :  path,
            type : 'GET',
            data: { "name": value,
                    "_token": $('meta[name="_token"]').attr('content')
            },

            beforeSend: function(){
                if(window[website] != null) {
                    window[website].abort();
                }
            },
            success: function (people) {
                status = true;
                resolve([status, people]);
            },

            error: function (jqXHR, textStatus, errorThrown) {
                reject([status, textStatus]);

            },

            timeout: 20000

        });
    });
}
我这样调用函数:

ajaxRequest('Jack', 'search/twitter', 'twitter').then(function(res) { console.log(res)}, function(err){console.log(err)});
ajaxRequest('Jack', 'search/instagram', 'instagram').then(function(res) { console.log(res)}, function(err){console.log(err)});
现在我需要知道这两个ajax请求已经完成。我该怎么做


注意到我认为我必须使用promise.all(),但不确定如何在我的情况下使用它。

你是对的,
promise.all()
就是为了解决这个问题而发明的。 它所做的只是返回一个新的承诺,该承诺将在所有给定的承诺都已解决时得到解决

在您的情况下,您可以使用
Promise来包装您的两个ajax调用

promise.all([
   ajaxRequest('Jack', 'search/twitter', 'twitter').then(function(res) { console.log(res)}, function(err){console.log(err)}),
   ajaxRequest('Jack', 'search/instagram', 'instagram').then(function(res) { console.log(res)}, function(err){console.log(err)})
]).then(([response1, response2]) => {
   // Here you execute your logic when both of the promises are resolved.
})

你说得对,
promise.all()
就是为了解决这个问题而发明的。 它所做的只是返回一个新的承诺,该承诺将在所有给定的承诺都已解决时得到解决

在您的情况下,您可以使用
Promise来包装您的两个ajax调用

promise.all([
   ajaxRequest('Jack', 'search/twitter', 'twitter').then(function(res) { console.log(res)}, function(err){console.log(err)}),
   ajaxRequest('Jack', 'search/instagram', 'instagram').then(function(res) { console.log(res)}, function(err){console.log(err)})
]).then(([response1, response2]) => {
   // Here you execute your logic when both of the promises are resolved.
})

您可以将函数调用传递给
$.when()
。注意,
jQuery.ajax()
返回一个jQuery承诺对象,不需要使用
promise
构造函数

$.when(ajaxRequest(), ajaxRequest())
.then(function(...results) {
  // do stuff with `results` array
})
.fail(function(jqxhr, textStatus, errorThrown) {
  console.error(errorThrown)
})

您可以将函数调用传递给
$.when()
。注意,
jQuery.ajax()
返回一个jQuery承诺对象,不需要使用
promise
构造函数

$.when(ajaxRequest(), ajaxRequest())
.then(function(...results) {
  // do stuff with `results` array
})
.fail(function(jqxhr, textStatus, errorThrown) {
  console.error(errorThrown)
})


在您的代码中,我实际上调用了该函数两次。一次正常(在我的代码中),一次进入
promise.all()
(在你的代码中)我忘了提到你需要用
promise来包装你的两个调用。所有的
都像在我的代码中一样。不,我需要单独得到这些ajax请求的结果,而不是在所有请求都完成后。好的,那么,你可以把你的代码(用
包装起来,然后
)在
Promise.all
,然后
Promise的
然后
。当两个ajax都被解析时,将调用all
。因此,我不需要
[response1,response2]
,我可以删除它吗?在您的代码中,我实际上调用了两次该函数。一次正常(在我的代码中),一次进入
promise.all()
(在你的代码中)我忘了提到你需要用
promise来包装你的两个调用。所有的
都像在我的代码中一样。不,我需要单独得到这些ajax请求的结果,而不是在所有请求都完成后。好的,那么,你可以把你的代码(用
包装起来,然后
)在
Promise.all
,然后
Promise.all的
然后
将在两个ajax都被解决时调用。所以,我不需要
[response1,response2]
,我可以删除它吗?您可以将函数调用传递给
$。when()
@guest271314 emm,不确定您的确切意思是什么,你有什么例子吗?你可以将函数调用传递给
$。when()
@guest271314 emm,不知道你的确切意思,你有什么例子吗?没有,我需要分别(异步)获得这些ajax请求的结果,而不是在完成所有操作的同时。我只需要知道所有这些都是为了停止闪烁放大镜。看。在每次异步或同步调用返回值之前,不应调用Chained
。then()
<如果任何
$.ajax()
调用被拒绝或抛出错误,则会到达code>.fail()
。如果要使每个调用分别异步,则问题代码的问题是什么?信念无关。自己尝试代码来确定结果。是的,如果您只需要在
.then()
上执行一个特定的任务,而不使用来自服务器的响应。@MartinAJ
.catch(err=>{console.error(err);stoppling();})
不,我需要分别(异步)获得这些ajax请求的结果,而不是在所有操作完成的同时。我只需要知道所有这些都是为了停止闪烁放大镜。看。在每次异步或同步调用返回值之前,不应调用Chained
。then()
<如果任何
$.ajax()
调用被拒绝或抛出错误,则会到达code>.fail()
。如果要使每个调用分别异步,则问题代码的问题是什么?信念无关。自己尝试代码来确定结果。是的,如果您只需要在
.then()
上执行不使用服务器响应的特定任务。@MartinAJ
.catch(err=>{console.error(err);stoppling();})