Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Forked from domenic/streams.js
Created March 19, 2012 20:02
Show Gist options
  • Save kriskowal/2126149 to your computer and use it in GitHub Desktop.
Save kriskowal/2126149 to your computer and use it in GitHub Desktop.
Streams are painful
function promiseResponse(request) {
var deferred = Q.defer();
request.on("error", deferred.reject);
request.on("response", deferred.resolve);
return deferred.promise
// if you must:
.fin(function () {
request.removeListener("error", deferred.reject);
request.removeListener("response", deferred.resolve);
});
}
function exists(id) {
return promiseResponse(knoxClient.head(id))
.then(function (response) {
return response.statusCode === 200;
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment