Angularjs $q.all Example - Jquery

What is $q promises in angularjs?

Snippet Code

Rate this page :
  [ 0 votes]

Angularjs promise is used to view the the built-in $q service. $q.all is a service which helps you to run function asynchronously and use their return value after they processed. The sample code using $q promises in angularjs is given below.

<script src=""></script> <script src=''></script> <script> function PromiseCtrl($scope, $q, $timeout) { var getPromise = function(delay) { var deferred = $q.defer(); $timeout(function() { if(delay > 0) { deferred.resolve("Success"); } else { deferred.reject("Fail"); } }, delay); return deferred.promise; }; $scope.result = "Waiting"; $q.all([ getPromise(1000), getPromise(2000), getPromise(3000) ]).then(function(value) { $scope.result = value; }, function(reason) { $scope.result = reason; }); } </script> <div ng-app> <div ng-controller="PromiseCtrl"> {{result}} </div> </div>


Ask Questions

Ask Question