0

I have two api and I want when both of api is complet,do another work, And the other question is that if I want to act as a chain, one should run first and then run the latter,in console is undefine write

  var promiseA = service.getAppReciverServiceList(function (data) {
    self.chargeMoney = data;


});
var promiseB = service.getAppReciverServiceList(function (data) {
    self.chargeMoney = data;

 return data;
});
setTimeout(function () { console.log(promiseB); }, 2000);

q.all([
    promiseA, promiseB
]).then(function (data) {
    console.log(data)
    //Array of result [resultOfgetServiceDetails1, resultOfgetServiceDetails2]
    scope.variable = data;
});
miladdn131
  • 72
  • 5

1 Answers1

0

I find my answer,

if we want use chain,mean one api response and then other api work:

  var promise = $q(function (resolve, reject) {
            service.getAppReciverServiceList(function (data) {
                console.log("1")
                resolve(data);
            })
           });

 promise.then(function (data) {
        console.log(data, "2")


    });

if we want all of api response and then do other work:

var deffered1 = $q.defer();
service.getAppReciverServiceList(function (data) {
    deffered1.resolve(data);
})
var deffered2 = $q.defer();
service.getAppReciverServiceList(function (data) {
    deffered2.resolve(data);

});

$q.all([deffered1.promise, deffered2.promise]).then

    (
    function (data) {
        console.log(data);
    },
    function () {
        console.log('an error occured');
        // error
    }

    );
miladdn131
  • 72
  • 5