Thursday, December 18, 2014

Using Q promises with node.js http requests

This post is intended at providing a code snippet on how to use Q promises with node.js http requests.

I was introduced to the concept of promises and deferred when I found myself uncomfortable writing asynchronous functions within callbacks.

The code is not:
1. Maintanable
2. Readable
3. Intuitive

That is when I stumbled on JQuery Deffered and Promises. Once I got a hang of it I came across the Q Library that does pretty much the same thing with node.js.

I found it difficult to apply it to node.js http requests and after I gave it more time I got a hang of it.

The following gist demonstrates on how to wrap the the node.js http request with the q library.

In the gist we are attempting to invoke the Google News API using the node.js http requests.


No comments:

Post a Comment