Created
November 12, 2015 06:03
-
-
Save YashdalfTheGray/19424d31a9315d150501 to your computer and use it in GitHub Desktop.
Promise progress notification using Angular's $q
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 getResource(url) { | |
var resource = []; | |
var def = $q.defer(); | |
var lastProgress = 0; | |
server.openAndGet(url, function(chunk) { | |
resource.push(chunk); | |
}); | |
while(!server.txDone()) { | |
if (server.getProgress() !== lastProgress) { | |
lastProgress = server.getProgress(); | |
def.notify(lastProgress); | |
} | |
} | |
if (server.txDone()) { | |
def.resolve(resource); | |
} | |
if (server.txError()) { | |
def.reject(server.getTxError()); | |
} | |
return def.promise; | |
} | |
getResource(myUrl) | |
.then(resolveHandler) | |
.catch(errorHandler) | |
.finally(finallyHandler, notifyHandler); | |
// There is another syntax for this that only uses .then() | |
getResource(myUrl).then( | |
resolveHandler, | |
errorHandler, | |
notifyHandler | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment