dragster
dragster

Reputation: 448

How to create a stream of response from an API request in Node.js?

I have been using the asynchronous abilities of Node.js from quite some time now. But I am stuck on an interesting problem. Basically I have 2 API's that I need to call one after the other. Due to the asynchronous nature of Node.js I cannot retrieve the response of the first API request till it has finished and the respective callback function is called. What I want to do is that I want to pass the response from the first API as request payload to the second API on the fly and not wait till the first API gets fully completed.

As a possible alternative, should I switch from building rest API to stream APIs?

Any pointers on how to do this?

Thanks

Upvotes: 2

Views: 4058

Answers (1)

Dreams
Dreams

Reputation: 6122

Yes, converting REST API'S to stream API is a better option. Node.js is known for its asynchronous behaviour. Because of the same all REST api's function in the same manner as you described earlier. As someone has previously pointed you could look at the Twitter Stream API for reference.

For more understanding you can check out this link - How to create a streaming API with NodeJS

Upvotes: 1

Related Questions