4 ms·
Good to see that they are adding promisified versions of more core function like fs. It makes things even simpler for new users especially with async/await supp
by talawahdotnet 8y ago
Good to see that they are adding promisified versions of more core function like fs. It makes things even simpler for new users especially with async/await support.
I really wish they would add a promise based version of the http/https client. The current version uses events rather than the standard callback signature so it can't be automatically converted using util.promisify().
They don't have to modify the existing low-level http client, just add a new API. They could even use the existing fetch() API that users are already familiar with on the client side.
It really should be dead simple for a new user to make an http call in Node without having to npm install a 3rd party library. I recently created an AWS Lambda function (directly via the console) and I just wanted to check to see what the outside world thought my IP was. In theory, with Node.js 8 It should only take a two liner like this:
const response = await fetch('https://httpbin.org/ip');
return response.json();
Instead I had to wrap it in a promise myself using untested code that I cribbed from StackOverflow[1].
1.https://gist.github.com/talawahtech/f8c4452dbb543297a008d3856ef47adb https://gist.github.com/talawahtech/f8c4452dbb543297a008d385...
- michaelsbradley 8y agoWith a little twist, you could make it asynchronously iterable, i.e. if you wanted to iterate over the 'data' chunks/events as they arrive: http://2ality.com/2018/04/async-iter-nodejs.html http://2ality.com/2018/04/async-iter-nodejs.html
- ilaksh 8y agoUse one of the fetch modules from npm.
- untog 8y agoThere's a module named `node-fetch` that mirrors the client Fetch API. Might make for a better solution. (but I agree it would be great for it to be built in)
- alacombe 8y agoI would not recommend `node-fetch', it's pretty crappy in term of performance if you are hitting a same host and expect some kind of keep-alive / pipelining. Switching to `make-fetch-happen' fixed all my issues.
- Can_Not 8y agoAlso if you're not intentionally looking to use a low level API, axios is much better.
- talawahdotnet 8y agoYea, built-in is what I was really going for. It would be great if new users could easily do a one-liner fetch without having to: a) Research and compare the various npm modules available and then install one (which isn't even possible via the Lambda console) or b) Figure out how to wrap an event based function in a promise (or search stack overflow and copy one) Something like this should be super easy to do from the cli/REPL or web console without dependencies. You should only have to install a module when you want more advanced features.
- ilkkao 8y agoBuiltin fetch was proposed recently, here is the issue: https://github.com/nodejs/node/issues/19393 https://github.com/nodejs/node/issues/19393
- emilsedgh 8y agoI'm quite disappointed by most of these fetch API's. They can do the easy tasks very nicely. But once you need to do a bit more, you need to use another package. request [0] has done everything I ever wanted it to do and request-promise-native [1] makes it async/friendly. [0] https://www.npmjs.com/package/request https://www.npmjs.com/package/request [1] https://www.npmjs.com/package/request-promise-native https://www.npmjs.com/package/request-promise-native