Skip to content

Instantly share code, notes, and snippets.

@xsurge83
Created June 12, 2015 17:50
Show Gist options
  • Save xsurge83/5f93eb42e84c21b5d559 to your computer and use it in GitHub Desktop.
Save xsurge83/5f93eb42e84c21b5d559 to your computer and use it in GitHub Desktop.
Last Promise
function requestApi() {
return $q.when();
}
var wrapApi = lastPromise(requestApi)
.then(function() {
// we are done here
});
function lastPromise(fn) {
var deferred, pendingEntries = 0;
return function() {
if (pendingEntries===0) {
deferred = $q.defer()
}
fn().then(requestWrap(++pendingEntries));
return deferred.promise;
}
function requestWrap(index){
return function(data){
if(index === entries){
deferred.resolve(data);
pendingEntries=0;
deferred = null;
}
}
}
}
wrapApi(), wrapApi()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment