Last active
December 16, 2015 22:28
-
-
Save mgonto/5506774 to your computer and use it in GitHub Desktop.
Enhanced Promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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