Skip to content

Instantly share code, notes, and snippets.

@mortezae
Forked from krnlde/promisify-http.js
Created February 15, 2023 19:40
Show Gist options
  • Save mortezae/0723572c1f6efc9320a98a7a61a541e5 to your computer and use it in GitHub Desktop.
Save mortezae/0723572c1f6efc9320a98a7a61a541e5 to your computer and use it in GitHub Desktop.
util.promisify.custom and http.get example
const http = require('http');
const {promisify} = require('util');
http.get[promisify.custom] = function getAsync(options) {
return new Promise((resolve, reject) => {
http.get(options, (response) => {
response.end = new Promise((resolve) => response.on('end', resolve));
resolve(response);
}).on('error', reject);
});
};
const get = promisify(http.get);
(async () => {
try {
const response = await get('http://krnl.de/');
let body = '';
response.on('data', (chunk) => body += chunk);
await response.end;
console.log(body);
} catch(e) {
// statements
console.log(e);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment