Created
November 20, 2019 11:43
-
-
Save thybzi/79f097e73647800bb3406fd4f23cb1d1 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
/** | |
* @param {string} url | |
* @returns {Promise<string>} | |
* @private | |
*/ | |
async function _fetchRemote(url) { | |
const http = /^https:/.test(url) ? require('https') : require('http'); | |
return new Promise((resolve, reject) => { | |
http.get(url, res => { | |
res.setEncoding('utf8'); | |
let body = ''; | |
res.on('data', (data) => { | |
body += data; | |
}); | |
res.on('end', () => { | |
resolve(body); | |
}); | |
}).on('error', (err) => { | |
reject(err); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage (await):
Usage (promise):