Srinivas Kothuri
Srinivas Kothuri

Reputation: 109

How to do asynchronous processing in nodejs

I'm a newbie in node js and trying to build a simple app to build web pages from RSS feeds.

http.get('http://www.someurl.com/news?output=rss', function(res) {
    res.on('data', function(chunk) {
        resLength += chunk.length; 
        chunks.push(chunk);
    });
    res.on('end', function(chunk) {
        // combine all chunks and process feed XML.
    });
})

Processing the server response may go longer and will block the main thread.

How can I create an async operation to process the intermediate server response and send the result back?

Upvotes: 0

Views: 98

Answers (2)

Vivek Pratap Singh
Vivek Pratap Singh

Reputation: 9964

In nodejs, you should not do stuff that uses extensive CPU time as it is single threaded server(event loop). Rest other stuff don't blocks the entire process. Server makes that request and continue doing other things, when the request comes back(after millions of clock cycles), you can execute the callback, all you need is the pointer to the callback.

therfore, You coudn't process asynchronously requests that has extensive CPU Usage. Apart from that, all other request works as if process asynchornously.

Upvotes: 1

have a look at expressjs.com, which is the entire world's go-to for node.js web apps, and the request package for getting http resources

Upvotes: 0

Related Questions