Last active
February 22, 2023 03:42
-
-
Save krnlde/797e5e0a6f12cc9bd563123756fc101f to your computer and use it in GitHub Desktop.
util.promisify.custom and http.get example
This file contains hidden or 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
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
awesome!!! (╹ڡ╹ )