代码之家  ›  专栏  ›  技术社区  ›  Crab Bucket tik27

为什么我的jQuery承诺会立即得到解决?

  •  0
  • Crab Bucket tik27  · 技术社区  · 6 年前

    使用jQuery承诺,我试图:

    1. 为(动物的)所有可能的值调用API
    2. 当每一个动物的声音回来时通知——比如说,这需要一段时间才能解决

    $.when() . 我希望当所有动物的声音都返回时,这个问题会得到解决,但我发现它会立即得到解决。有人知道我做错了什么吗?

    function () {
      $('#txtNotification').text('Started ....');
    
      $.ajax({
        url: "/api/animals/all"
      }).done(function(data) {
        var animalFunctions = [];
    
        for (var animalType of data) {
          var animalFunction = $.ajax({
            url: "/api/animal/sound/" + animalType
          }).done(function(data) {
            $('#txtNotification').text(data);
          });
    
          animalFunctions.push(animalFunction);
        }
    
        $.when(animalFunctions).then(function() {
          $('#txtNotification').text('Done.');
        });
      });
    }
    
    3 回复  |  直到 6 年前
        1
  •  1
  •   MTCoster    6 年前

    $.when() 是为数不多的jQuery函数之一 cannot accept an array -您需要将每个承诺作为一个单独的论据:

    ES6方式:

    $.when(...animalFunctions).then(() => {
      $('#txtNotification').text('Done.');
    });
    

    $.when.apply($, animalFunctions).then(function () {
      $('#txtNotification').text('Done.');
    });
    
        2
  •  0
  •   Yom T.    6 年前

    jQuery的 Deferred.promise()

    var $def = jQuery.Deferred();
    
    $.ajax({
      url: "https://jsonplaceholder.typicode.com/users"
    })
    .done(function getUsersAsync(users) {
      for (var user of users) {
        $def.notify(`Fetching comments of ${user.username}, ID: ${user.id}`);
        
        $.ajax({
          url: "https://jsonplaceholder.typicode.com/comments",
          data: JSON.stringify({ id: user.id })
        })
        .done(function (arg) {
          // Do stuff with the comments here
        });
      }
      
      $def.resolve(users.length);
    })
    .fail(function () {
      $def.reject('ERR: Failed retrieving comments');
    });
    
    
    $def
      .progress(function (message) {
        console.log(message);
      })
      .done(function (count) {
        console.log('ALL DONE. Total user: ' + count);
      });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        3
  •  -3
  •   Jay    6 年前

    添加 return: false

    function () {
            $('#txtNotification').text('Started ....');
    
            $.ajax({
                    url: "/api/animals/all"
                })  
                .done(function( data ) {
    
                    var animalFunctions = [];
    
                    for (var animalType of data) {
    
                        var animalFunction = $.ajax({
                                url: "/api/animal/sound/" + animalType
                            })
                            .done(function(data) {
                                $('#txtNotification').text(data);
                                return false;
                            }
                        );
    
                        animalFunctions.push(animalFunction);
                    }
    
                    $.when(animalFunctions).then(function() {
                        $('#txtNotification').text('Done.');
                    });
                });
        }