thetallweeks
12/1/2015 - 1:42 AM

no-deferred.js

// currently
function update(id, deployment, local) {
  var deferred = $q.defer(),
    d = $q.defer();
    
  if (!local) {
    $http.patch('/api/deployments/' + id, deployment).success(function (data) {
      if (_.isObject(data)) {
        d.resolve(deployment);
      } else {
        deferred.reject(data);
      }
    }).error(function () {
      deferred.reject();
    });
  } else {
    d.resolve(deployment);
  }

  d.promise.then(function (deployment) {
    var idx = _.findIndex(scope.deployments, { id: id });
    var full = _.extend({ id: id }, deployment);

    scope.deployments[idx] = full;
    var cloned = _.cloneDeep(full);
    deferred.resolve(cloned);
    $rootScope.$emit('deploymentsService.update', id, cloned);
  });

  return deferred.promise;
}

// can be written as
function update(id, deployment, local) {
  return !local ? cache.set(deployment, { sync: true }) : cache.set(deployment);
}