AngularJs $ q.defer () not working

I had problems with $q.defer();
When I used callbacks , my code worked (the update was updated), but with $q.defer(); it is not .

This is my code:
Service:

 eventsApp.factory('eventData', function($http, $q) { return { getEvent: function(callback) { var deferred = $q.defer(); $http({method: 'GET', url: '/node/nodejsserver/server.js'}). success(function(data, status, headers, config){ //callback(data.event); deferred.resolve(data.event); console.log('status: ', status, ' data: ', data); }). error(function(data, status, headers, config){ deferred.reject(status); console.log('status: ', status); }); return deferred.promise; } }; }); 

Controller:

 eventsApp.controller('EventController', function EventController($scope, eventData) { $scope.event = eventData.getEvent(); } ); 

But that will not work.

Then I found this answer , and I updated my controller as follows:

 eventsApp.controller('EventController', function EventController($scope, eventData) { eventData.getEvent().then(function(result) { $scope.event = result; }); } ); 

and it works.
What is the difference between non-working and working code?

+5
source share
2 answers

Inoperative code uses automatic neutralization of promises, which has been deprecated and removed in recent versions of Angular. It was considered too magical.

Angular used when you returned a promise:

  • Returns an empty array.
  • Fill it out later when the request arrives.
  • Running a digest on its own.

This behavior was considered confusing and magical by Angular developers and was deprecated (in 1.2), deactivated, and soon (1.3) removed in Angular. The correct way to assign a value is through a promise, as you pointed out in the second example:

 eventData.getEvent().then(function(result) { $scope.event = result; }); 

From the Angular 1.3 (pending) release docs:

$ parse: due to fa6e411d , the disposal of promises has been removed. It is deprecated with 1.2.0-rc.3. It can no longer be turned on. Two methods have been removed:

And from the documents of release 1.2:

$ parse and templates generally will no longer automatically unpack promises.

Before:

$scope.foo = $http({method: 'GET', url: '/someUrl'}); <p>{{foo}}</p>

After:

$http({method: 'GET', url: '/someUrl'})

.success(function(data) { $scope.foo = data; });``

{{Foo}}

`

This feature is deprecated. If absolutely necessary, it can now be re-enabled through the $ parseProvider.unwrapPromises (true) API.

While we are avoiding a deferred anti-pattern here , $http already returning a promise, so you can simply return instead of using $q.defer .

+11
source

Edit: check Benjamin's answer

In your service, you return a promise object, the promise object has a .then method.

you assign the promise object to $scope.event , so you won’t get the data (in the latest version according to Benjamin)

when you resolve a promise with deferred.resolve(data.event) , then the function that you passed as the .then argument is called with that resolved data.

You can give a second .then argument that will be called when you do deferred.reject()

This is the basic functionality of the promise api. Just read the docs for more info https://docs.angularjs.org/api/ng/service/ $ q

+1
source

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


All Articles