Pass in an array of Deferreds to $.when()

后端 未结 9 1394
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 22:20

Here\'s an contrived example of what\'s going on: http://jsfiddle.net/adamjford/YNGcm/20/

HTML:

Click me!
&l
9条回答
  •  佛祖请我去吃肉
    2020-11-21 22:36

    If you're using angularJS or some variant of the Q promise library, then you have a .all() method that solves this exact problem.

    var savePromises = [];
    angular.forEach(models, function(model){
      savePromises.push(
        model.saveToServer()
      )
    });
    
    $q.all(savePromises).then(
      function success(results){...},
      function failed(results){...}
    );
    

    see the full API:

    https://github.com/kriskowal/q/wiki/API-Reference#promiseall

    https://docs.angularjs.org/api/ng/service/$q

提交回复
热议问题