Skip to content

Instantly share code, notes, and snippets.

@domenic
Created March 19, 2012 19:35
Show Gist options
  • Save domenic/2125448 to your computer and use it in GitHub Desktop.
Save domenic/2125448 to your computer and use it in GitHub Desktop.
Streams are painful
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