0

I have an array with url's

var urls = ['http://google.com', 'http://example.com', 'http://localhost:300'];

and a function that reads from url

function readUrl(url) {....}

i wan't to run the function for all available urls in the array. Is that possible with asyncjs? or any other suggestion.

4
  • Your question could be interpreted as meaning you just want to run some function for each element of an array, which is just urls.forEach(readUrl). Presumably you're interested in doing something more interesting, such as running them in sequence, or running them in parallel and waiting until all they finish, or...?? Also, what does readUrl do--does it return a promise, because otherwise, with no callback visible in your example, there will be no way to tell if it's done at all. Commented Jun 30, 2016 at 14:24
  • @torazaburo This is resolved over a year ago :D Commented Jun 30, 2016 at 14:36
  • dear downvoters care to comment please., at least strange downvote for answer which was accepted.. Commented Jun 30, 2016 at 15:00
  • @torazaburo as I understood you downvoted for my answer, could you descrie why you have done it? Commented Jun 30, 2016 at 15:15

1 Answer 1

1

Is that possible with asyncjs?

yes, it is possible., like this

1.

var async = require('async');
var urls  = ['http://google.com', 'http://amazon.com', 'http://bing.com'];

function readUrl(url, next) {
  // some code  
  next(null, result);
}

urls = urls.map(function(url) {
  return function (next) {
    readUrl(url, next);
  };
});

async.parallel(urls, function(err, res) {
  console.log(err, res);
})

2.

var async = require('async');
var urls = ['http://google.com', 'http://amazon.com', 'http://bing.com'];

function readUrl(url) {
  return true;
}

urls = urls.map(function(url) {
  return function (next) {
    var res = readUrl(url);

    if (res) {
      return next(null, res);
    }

    next('Error');
  };
});

async.parallel(urls, function(err, res) {
  console.log(err, res);
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.