Skip to content

Instantly share code, notes, and snippets.

@mikeal
Last active April 21, 2023 17:13
Show Gist options
  • Save mikeal/013dbb2ef3810cfe976cbe16ddb11c6f to your computer and use it in GitHub Desktop.
Save mikeal/013dbb2ef3810cfe976cbe16ddb11c6f to your computer and use it in GitHub Desktop.
HTTP Client Comparison
const r2 = require('r2')
let doJsonThing = async (path, propname) => {
let res = await r2(`http://api.com${path}`).json
return res[propname]
}
const request = require('request')
let doJsonThing = (path, propname, cb) => {
request(`http://api.com${path}`, {json: true}, (err, resp, body) => {
if (err) return cb(err)
if (resp.statusCode < 200 || resp.statusCode > 299) {
return cb(new Error(`Status not 200, ${resp.statusCode}`))
}
cb(null, body[propname])
}
}
@jakearchibald
Copy link

jakearchibald commented Aug 31, 2017

@mikeal there's also response.ok which maps to what you wrote. https://fetch.spec.whatwg.org/#ref-for-dom-response-ok①.

Wait I'm being an idiot, that was the old example. Ignore me.

@darrentorpey
Copy link

Thanks for the detailed explanation, @mikeal -- interesting PoC

@mrpeu
Copy link

mrpeu commented Sep 1, 2017

I want to believe you; I feel like there is something powerful I'm missing. But I don't understand your answer to @bluepnume and @bahmutov.
@jakearchibald: I'll take over the idiot badge ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment