Skip to content

Instantly share code, notes, and snippets.

@coverslide
Created March 28, 2021 08:57
Show Gist options
  • Save coverslide/2d2595e12648799d4d42f0f4f9d0da0b to your computer and use it in GitHub Desktop.
Save coverslide/2d2595e12648799d4d42f0f4f9d0da0b to your computer and use it in GitHub Desktop.
const fetch = (url) => {
return new Promise((resolve, reject) => {
https.get(url, (response) => {
const dataPromise = new Promise((resolve, reject) => {
const d = [];
let dlen = 0;
response.on('error', reject);
response.on('data', (data) => {
d.push(data)
dlen += data.length;
})
response.on('end', () => {
resolve(Buffer.concat(d, dlen));
})
});
const getJson = () => dataPromise.then(data => JSON.parse(data.toString()));
resolve({
json: getJson
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment