Skip to content

Instantly share code, notes, and snippets.

@CacheControl
Created March 22, 2019 15:25
Show Gist options
  • Save CacheControl/5ca97592d8b070ac8c21b520ff8dceda to your computer and use it in GitHub Desktop.
Save CacheControl/5ca97592d8b070ac8c21b520ff8dceda to your computer and use it in GitHub Desktop.
node.js http request to promise
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