javascript - Node.js: Best way to perform multiple async operations, then do something else? -


In the following code I am trying to create multiple (about 10) HTTP requests and RSS pars at once.

Code:

I

code> Miscellaneous articles; Feedstofet. Each (function (feed) (feed, function (mistake, feed article) {if (mistake) {throw it;} other {article = articles.concact (feedartics);}});}); // code I want to run once after visiting all the feeds of the url

I understand that while using a callback I used a callback in this example while calling the function The only way to think about doing a call is to call a function that believes that how many times it has been called and continues only when it feedsToFetch.length is hacked.

So my question is, what is the best way to handle this type of situation in node.js .

Possibly without any form of blocking! (I still want that fast speed is fast) Is this promise or something?

Thanks, Danny

hack-free solution

Then do something else) This is the perfect match for your scenario

Bluebird also has .map () which can take an array of values ​​and start a promise chain

Here Bluebird .map () : < pre> var is an example using promise = require ('bluebird'); Var Requests = Promise.promisifyAll (Required ('Request')); Function ProcessFilffeds (Feedstofetch) {Promise.map} (Feedsofoffeach, Function (Feed) {// I changed your 'Feed' FN to 'Processed' Return ProcessFeed (Feed)} Then (function (article) {/ / 'The article' now has an array w / call all the results of 'processes' / something with all the results ...}). (Function (e) {// feed server was down, etc}}} function processfid (Feed) {// // Use the promisified version of 'get' return request.getAsync (feed.url) ...}

Also note that you do not need to use closures here to submit results.

With many examples, really well written So, it is easy to take it

Once I have learned the promise pattern, life has made it so easy. I can not recommend it adequately

Besides, Regarding various approaches to dealing with tasks, as ync module, and others

Hope this helps!

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -