meumobi / ng-meumobi-utils

0 stars 1 forks source link

Remove useless $q.defer() #11

Open vdias38 opened 7 years ago

vdias38 commented 7 years ago

Convert from callbacks

Why ?

The ES6 Promise constructor would look the same Promise(function(resolve, reject){ ... });

The deferred object from $q.defer() comes with the .notify() method, compared to the $q(function(resolve, reject) { ... }) syntax which does not pass in a notify callback to use.

      var d = $q.defer();      
      $window.analytics.setUserId(id, function (response) {
        d.resolve(response);
      }, function (error) {
        d.reject(error);
      });

      return d.promise;

Prefer following code

return $q(function(resolve, reject) { 
   $window.analytics.setUserId(id, function (response) {
        resolve(response);
      }, function (error) {
        reject(error);
      });
 })

when to use defer would be nice.

An use for $q.defer is for cancel a request

Source: http://www.codelord.net/2015/09/24/%24q-dot-defer-youre-doing-it-wrong/