Created
March 19, 2012 19:35
-
-
Save domenic/2125448 to your computer and use it in GitHub Desktop.
Streams are painful
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 existsAsync(id) { | |
var deferred = Q.defer(); | |
var clientRequest = knoxClient.head(id); | |
clientRequest.on("error", deferred.reject); | |
clientRequest.on("response", function (clientResponse) { | |
function onEnd() { | |
cleanup(); | |
deferred.resolve(clientResponse.statusCode === 200); | |
} | |
function onError(error) { | |
cleanup(); | |
deferred.reject(error); | |
} | |
function cleanup() { | |
clientResponse.removeListener("end", onEnd); | |
clientResponse.removeListener("error", onError); | |
} | |
clientResponse.on("end", onEnd); | |
clientResponse.on("error", onError); | |
}); | |
return deferred.promise; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment