Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise-based node http framework?

Node frameworks usually work via (err, result) callbacks.

Is there a promise-based http framework for Node, with a healthy community and in active development (such as express)?

like image 231
slezica Avatar asked Apr 05 '13 00:04

slezica


People also ask

What is Promise based HTTP client?

A promise-based client returns promises rather than accepting callbacks. Follow this answer to receive notifications.

What is a Promise node?

A Promise in Node means an action which will either be completed or rejected. In case of completion, the promise is kept and otherwise, the promise is broken. So as the word suggests either the promise is kept or it is broken. And unlike callbacks, promises can be chained.

What is Axios HTTP?

Axios is a promised-based HTTP client for JavaScript. It has the ability to make HTTP requests from the browser and handle the transformation of request and response data.

Does Nodejs have promises?

A Node. js Promise is a placeholder for a value that will be available in the future, allowing us to handle the result of an asynchronous task once it has completed or encountered an error. Promises make writing asynchronous code easier. They're an improvement on the callback pattern and very popular in Node.


2 Answers

There is https://github.com/mzabriskie/axios

Promise based HTTP client for the browser and node.js

Example code:

// Make a request for a user with a given ID
axios.get('/user?ID=12345')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (response) {
    console.log(response);
  });
like image 95
wires Avatar answered Sep 22 '22 03:09

wires


I had the same question you had today, and I found q-io, also by kriskowal. It uses Q.js to promise-wrap filesystem io as well as server and http client.

I have not tried it out yet, but it definitely does not seem to have an active community built around it at this point. Sad thing to see, in a world filled with callback pyramids of doom.

like image 4
chinnychinchin Avatar answered Sep 25 '22 03:09

chinnychinchin