Created
June 12, 2015 17:50
-
-
Save xsurge83/5f93eb42e84c21b5d559 to your computer and use it in GitHub Desktop.
Last Promise
This file contains hidden or 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 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