Skip to content

Instantly share code, notes, and snippets.

@alexjpaz
Last active February 9, 2019 12:50
Show Gist options
  • Save alexjpaz/ee3254140c7f15c873f077694286807d to your computer and use it in GitHub Desktop.
Save alexjpaz/ee3254140c7f15c873f077694286807d to your computer and use it in GitHub Desktop.
// async /await
const request = async (url) => {
let client = require('http');
if(url.startsWith('https')) {
client = require('https');
}
await new Promise((res, rej) => {
client.get(url, (rsp) => {
rsp.on('end', () => {
if(rsp.statusCode === 200) {
res();
} else {
rej();
}
});
})
.on('error', rej);
});
};
const main = async (url) => {
try {
await request(url);
console.log('success');
} catch(e) {
console.error(e);
process.exit(1);
throw e;
}
};
main("http://gooddgle.com");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment