Answer the question
In order to leave comments, you need to log in
What are your comments on this code? How to write correctly?
Am I reporting errors correctly? How would you write this code?
const http = require('http');
async function getBody(url) {
return new Promise((resolve, reject) => {
http.get(url, (res) => {
if (res.statusCode !== 200) {
throw new Error('Request Failed.\n' +
`Status Code: ${res.statusCode}`);
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
return resolve(rawData);
});
});
});
}
(async () => {
try {
let body = await getBody('http://site.com/');
console.log(body);
} catch (err) {
console.error(err);
}
})();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question