Created
March 28, 2021 08:57
-
-
Save coverslide/2d2595e12648799d4d42f0f4f9d0da0b to your computer and use it in GitHub Desktop.
This file contains 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) => { | |
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