使用1个或多个jQuery Promise [英] Working with 1 or more jQuery promises

查看:49
本文介绍了使用1个或多个jQuery Promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行1个或多个REST/ajax调用以验证某些用户信息.其余的电话运行良好,信息又回来了.我面临的问题与代码的那部分无关,它看起来像这样.

I'm making either 1 or more REST/ajax calls to validate some user information. The rest calls are working well and the information is coming back. The issue I'm facing isn't with that part of the code, which looks something like this.

function ensureUsers(recipients){
  var promises = [];
  for(var key in recipients){
      var payload = {'property':recipients[key]};
      promises.push( $.ajax({...}));
  }
  return $.when.apply($,promises);    
}

....

ensureUsers(users) // users is an array of 1 or more users
  .done(function(){
     console.log(arguments);
  )}

如果初始数组中有多个用户,则我的.done代码中的参数的结构如下:

If there is more than one user in the initial array, then the arguments in my .done code are structured like this:

[[Object,"success",Object],[Object,"success",Object]...]

然后我可以遍历每个结果,检查状态,然后继续.

I can then iterate over each result, check the status, and proceed.

但是,如果初始数组中只有一个用户,那么.done会获得如下参数:

However if there is only one user in the initial array then .done gets arguments like this:

[Object,"success",Object]

对我来说,奇怪的是,返回的内容的结构会像这样改变.我找不到关于这个特定问题的任何信息,所以我找到了一个解决方案

It seems strange to me that the structure of what is returned would change like that. I couldn't find anything about this specific a problem, so I hacked together a solution

var promises = Array.prototype.slice.call(arguments);
if(!Array.isArray(promises[0])){
    promises = [promises];  
}

那真的是我所能期望的最好的吗?还是有更好的方法来处理jQuery中1个或更多ajax调用返回的promise?

Is that really the best I can hope for? Or is there some better way to deal with the returned promises from 1 or more ajax calls in jQuery?

推荐答案

对我来说,奇怪的是,返回的内容的结构会像这样改变.

It seems strange to me that the structure of what is returned would change like that.

是的,jQuery在这里非常不一致.当您将单个参数传递给$.when时,它将尝试将其转换为一个承诺,当您传递多个参数时,它将突然尝试等待所有参数并将其结果组合在一起.现在说明jQuery Promise可以使用多个值(参数)进行解析,并为此添加特殊情况.

Yes, jQuery is horribly inconsistent here. When you pass a single argument to $.when, it tries to cast it to a promise, when you pass multiple ones it suddenly tries to wait for all of them and combine their results. Now throw in that jQuery promises can resolve with multiple values (arguments), and add a special case for that.

所以我建议两种解决方案:

So there are two solutions I could recommend:

  • 完全拖放$.when,只需使用

  • Drop $.when completely and just use Promise.all instead of it:

var promises = [];
for (var p of recipients) {
    …
    promises.push( $.ajax({…}));
}

Promise.all(promises)
.then(function(results) {
     console.log(results);
})

  • 使每个promise解析仅使用一个值(与$.ajax()解析为3的方式不同),以便它们不会被包装在数组中,并且$.when将产生一致的结果,而不考虑参数的数量:

  • Make each promise resolve with only a single value (unlike $.ajax() that resolves with 3) so that they don't get wrapped in an array, and $.when will produce consistent results regardless of number of arguments:

    var promises = [];
    for (var p of recipients) {
        …
        promises.push( $.ajax({…}).then(function(data, textStatus, jqXHR) {
            return data;
        }) );
    }
    
    $.when.apply($, promises)
    .then(function() {
         console.log(arguments);
    })
    

  • 这篇关于使用1个或多个jQuery Promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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