Last active
November 3, 2018 11:54
-
-
Save Conduitry/dd2708d0414dd5e87253c6dcd2fb7229 to your computer and use it in GitHub Desktop.
Node.js http[s].request helper
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
// ClientRequest -> Promise<IncomingMessage> with an extra .body field that is a Buffer of the entire response | |
const response = request => | |
new Promise((resolve, reject) => | |
request | |
.once('response', response => { | |
const chunks = []; | |
response.on('data', chunk => chunks.push(chunk)).once('end', () => { | |
response.body = Buffer.concat(chunks); | |
resolve(response); | |
}); | |
}) | |
.once('error', error => reject(error)), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment