Created
March 22, 2019 15:25
-
-
Save CacheControl/5ca97592d8b070ac8c21b520ff8dceda to your computer and use it in GitHub Desktop.
node.js http request to promise
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 http = require('http'); | |
return new Promise((resolve, reject) => { | |
http.get(url, (res) => { | |
if (res.statusCode >= 400) { | |
const err = new Error(`${res.statusCode} - ${res.statusMessage}`); | |
err.statusCode = res.statusCode; | |
return reject(err); | |
} | |
return resolve(res); | |
}).on('error', err => reject(err)); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment