Put promises in order

I am having trouble returning promises using the $ q # all method .

I want to make promises dependent on each other, that is:

If I install obj1, obj2 and obj3, I want to get them in the same order .

How can i achieve this?

Factory:

mainFactory.$inject = ['$http', '$q'];

function mainFactory($http, $q) {
  var mainFactory = {
    getPromises: getPromises
  };

  return mainFactory;

  function getPromises(id) {
    promises = {
      'obj1': $http.get('http1'),
      'obj2': $http.get('http2'),
      'obj3': $http.get('http3'),
      'obj4': $http.get('http4', { params: { 'id': id } }),
      'obj5': $http.get('http5'),
      'obj6': $http.get('http6', { params: { 'id': id } })
    };

    return $q.all(promises);
  }
}

Controller:

MainCtrl.$inject = ['mainFactory'];

function MainCtrl(mainFactory) {
  var vm = this;
  mainFactory.getPromises(id)
    .then(getResponse)
    .catch(getError);

  function getResponse(response) {
    var keys = Object.keys(response), i = keys.length;
    while (i--) {
      var key = keys[i];
      console.log(key); // I want all the keys in order, i.e. => obj1, obj2.. and so on
      var value = response[key].data;
      switch(key) {
        ...
      }
    }
  }

  function getError(error) {
    console.log(error);
  }
}

EDIT:

I tried also:

var promises = {};
return $http.get('/admin/http1.json').then(function (value) {
    promises['obj1'] = value;
  })
.then(function (result) {
    return $http.get('/admin/http2.json').then(function (value) {
    promises['obj2'] = value;
  });
}).then(function (result) {
    return $http.get('/admin/http3.json').then(function (value) {
    promises['obj3'] = value;
  });
});     
return $q.all(promises);
+4
source share
2 answers

Edit 2

Error, I just copied the code above, not realizing that this is an object. Lol

promises = [
  $http.get('http1'),
  $http.get('http2'),
  $http.get('http3'),
  $http.get('http4', { params: { 'id': id } }),
  $http.get('http5'),
  $http.get('http6', { params: { 'id': id } })
]

Change 1

Sorry, I didn’t notice the comments of Jared Smith .

Object keys are inherently disordered. Use an array instead.

Change 0

. promises.

promises = [
  $http.get('http1'),
  $http.get('http2'),
  $http.get('http3'),
  $http.get('http4', { params: { 'id': id } }),
  $http.get('http5'),
  $http.get('http6', { params: { 'id': id } })
]

$q.all(promises)
  .then(functions(resolves){
      // resolves here is an array
  }).catch(function(err){ 
      // throw err 
  });
+1

$q.all . , , .

function getPromises(id) {
  var getObjA = function () {
    return $http.get('http1');
  };

  var getObjB = function () {
    return $http.get('http2');
  };

  var getObjC = function () {
    return $http.get('http3');
  };

  var getObjD = function () {
    return $http.get('http4', { params: { 'id': id } });
  };

  var getObjE = function () {
    return $http.get('http5');
  };

  var getObjF = function () {
    return $http.get('http5', { params: { 'id': id } });
  };

  return getObjA()
    .then(getObjB)
    .then(getObjC)
    .then(getObjD)
    .then(getObjE)
    .then(getObjF);
}

: , catch

getPromises("id")
   .then(<success callback here>)
   .catch(<error callback that will catch error on any of the promises>);

, , promises catch

+1

Source: https://habr.com/ru/post/1649559/


All Articles