Skip to content

Instantly share code, notes, and snippets.

@wffranco
Last active January 5, 2023 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wffranco/bac8c044655198091b93c7881dfb8336 to your computer and use it in GitHub Desktop.
Save wffranco/bac8c044655198091b93c7881dfb8336 to your computer and use it in GitHub Desktop.
node.js fetch get
const fetch = url => new Promise((resolve, reject) => {
https.get(url, res => {
res.setEncoding('utf8');
let result = null;
res.on('data', data => {
result = JSON.parse(data);
});
res.on('end', () => {
if (res.statusCode != 200) {
reject("Error code " + res.statusCode);
} else {
resolve(result);
}
});
}).on('error', error => {
reject(error);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment