javascript - Angularjs + Promise -


i using angularjs, have 2 api in method in factory method. tried access 2 method 1 one. not working.

we need first print test1 print test2.

controller code:

app.controlle("test").function($scope, factorymethod) {     factorymethod.test1().then(function(data) {          console.log(data);         console.log("test1");      }).then(factorymethod.test2().then(function(data) {         console.log(data);         console.log("test2");      })).catch(function(data) {         alert(data);     }); } 

factory code:

app.factory("factorymethod", function (){     //code test1      //code test2 }); 

currently console log print following order: 1. test2 2. test1

expecting test1 , test2

it can done this. @ snippet

var app = angular.module('myapp',[]);    app.factory("factorymethod",['$q',function($q){        var  test1 = function(){       var deferred = $q.defer();                 deferred.resolve("test1");                        return deferred.promise;      }          var  test2 = function(){        var deferred = $q.defer();                 deferred.resolve("test2");                        return deferred.promise;      }          return {        test1 : test1,        test2 : test2      }  }]);    app.controller("test",function($scope, factorymethod){        var promise =  factorymethod.test1();        promise.then(function(data){          console.log(data);            factorymethod.test2().then(function(data){      console.log(data);          },function(error){      console.log("error test 2");  })        },function(error){     console.log("error test 1")  })       });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  <div ng-app="myapp" ng-controller="test">      </div>  


Comments