Last active
January 5, 2023 15:35
-
-
Save wffranco/bac8c044655198091b93c7881dfb8336 to your computer and use it in GitHub Desktop.
node.js fetch get
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 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