How to resolve $q.all?

狂风中的少年 提交于 2019-12-01 03:49:39

$q.all creates a promise that is automatically resolved when all of the promises you pass it are resolved or rejected when any of the promises are rejected.

If you pass it an array like you do then the function to handle a successful resolution will receive an array with each item being the resolution for the promise of the same index, e.g.:

  var resolveTopics = function() {
    $q.all([getToken(), getUserId()])
    .then(function(resolutions){
      var token  = resolutions[0];
      var userId = resolutions[1];
    });
  }

I personally think it is more readable to pass all an object so that you get an object in your handler where the values are the resolutions for the corresponding promise, e.g.:

  var resolveTopics = function() {
    $q.all({token: getToken(), userId: getUserId()})
    .then(function(resolutions){
      var token  = resolutions.token;
      var userId = resolutions.userId;
    });
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!