Skip to content

Instantly share code, notes, and snippets.

@mgonto
Last active December 16, 2015 22:28
Show Gist options
  • Save mgonto/5506774 to your computer and use it in GitHub Desktop.
Save mgonto/5506774 to your computer and use it in GitHub Desktop.
Enhanced Promises
function enhancePromise(promise, isCollection) {
promise.call = angular.bind(promise, promiseCall);
promise.get = angular.bind(promise, promiseGet);
return promise;
}
function promiseCall(method) {
var deferred = $q.defer();
var callArgs = arguments;
this.then(function(val) {
var params = Array.prototype.slice.call(callArgs, 1);
var func = val[method];
var result = func.apply(val, params);
deferred.resolve(result);
});
return enhancePromise(deferred.promise);
}
function promiseGet(what) {
var deferred = $q.defer();
this.then(function(val) {
deferred.resolve(val[what]);
});
return enhancePromise(deferred.promise);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment