Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for a promise?

I have the following angularjs code:

$scope.clients = commonFactory.getData(clientFactory.getClients()); if ($scope.clients.length > 0) {     $scope.sampleForm.ClientId = $scope.clients[0].ClientId; } 

And the getData function in commonFactory:

factory.getData = function (method) {     method.then(function (response) {         return response.data;     }, function (error) {         $rootScope.alerts.push({ type: 'error', msg: error.data.ExceptionMessage });     }); }; 

The problem is that $scope.clients.length is undefined when it hits that line because of the async call.

Is there a way to not do my length check until I know that $scope.clients has been assigned? I've looked at something like this:

$scope.clients = commonFactory.getData(clientFactory.getClients()).then(function () {     if ($scope.clients.length > 0) {         $scope.sampleForm.ClientId = $scope.clients[0].ClientId;     } }); 

Trying to chain my then promises, but no dice... the goal here is to have the getData method to avoid a bunch of boilerplate code for catching errors... maybe I'm going about this wrong?

like image 590
Nicros Avatar asked Sep 11 '13 22:09

Nicros


People also ask

How do you wait for a promise?

The keyword await is used to wait for a Promise. It can only be used inside an async function. This keyword makes JavaScript wait until that promise settles and returns its result. Here is an example with a promise that resolves in 2 seconds.

What does awaiting a promise do?

Awaiting a promise to be fulfilled If a Promise is passed to an await expression, it waits for the Promise to be fulfilled and returns the fulfilled value.

Does then wait for promise to resolve?

You can use the async/await syntax or call the . then() method on a promise to wait for it to resolve. Inside of functions marked with the async keyword, you can use await to wait for the promises to resolve before continuing to the next line of the function.

Does await return a promise?

Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.


1 Answers

This is the most basic situation that promises are for. You simply need to make a promise with var deferred = $q.defer() when beginning an async operation, resolve the promise with deferred.resolve(result) when the async operation is complete, and return deferred.promise in your function. Angular's asynchronous methods do this internally and return promises already, so you can just return those same promises rather than creating new promises with $q.defer(). You can attach a .then to anything that returns a promise. Further, if you return a value from a then function, that value will be wrapped in a promise so that the then chain can continue

angular.module('myApp', [])  .factory('myService', function($q, $timeout, $http) {   return {     myMethod: function() {       // return the same promise that $http.get returns       return $http.get('some/url');     }   }; })  .controller('myCtrl', function($scope, myService) {   myService.myMethod().then(function(resp) {     $scope.result = resp.data;   }); }) 

And here is a bit more fun with the chaining:

.factory('myService', function($q, $timeout, $http) {   return {     myMethod: function() {       // return the same promise that $http.get returns       return $http.get('some/url').then(function() {         return 'abc';       });     }   }; })  .controller('myCtrl', function($scope, myService) {   myService.myMethod().then(function(result) {     console.log(result); // 'abc'     return someOtherAsyncFunc(); // for example, say this returns '123'   }).then(function(result) {     console.log(result); // '123'   }); }) 
like image 66
m59 Avatar answered Oct 02 '22 07:10

m59