I don't know the async lib, but rolling your own map function is very
easy.

See this utility I bundle with my new safeRreturn micro-library.
https://github.com/creationix/safereturn/blob/master/safereturn.js#L68-94

Also I have few questions and comments.

Why does the small function take an callback?  toLowerCase is a sync
function and can be used with the built-in Array.prototype.map function.
 No need for an async map helper here.

    var names = ["A", "B"];
    names = names.map(function (char) { return char.toLowerCase(); });

Also in your pseudo code you say that for each item you want to download
stuff and then send emails.  What's the dependencies on these?  Can you
send emails as the downloads finish or do you need to wait till they are
all done?

The reason I ask is if sending an email only depends on a single download
result then there is no reason to nest loops.  Just compose the download
and send functions and do one loop.  This will require less waiting and
syncing and make your app faster and more parallel.

// Using a simple map function like from safeReturn
var onItem = map(onDone, names.length);
names.forEach(function (name) {
  downloadStuff(name, function (err, data) {
    if (err) return onDone(err);
    sendEmail(name, data, onItem(name));
  });
});


On Tue, Apr 17, 2012 at 10:53 AM, Mario Konschake <[email protected]
> wrote:

> Hello,
>
> I am new to node.js and try to do the following:
>
> read items from file (database entries)
>  for each item do expensive I/O operation (download stuff)
>  with the results do another expensive I/O operation (send mail)
>
> my approach in node.js involves map from async and looks like this:
>
> var async = require("async");
> database.read(arg, function (error, results) {
>  async.map(results, downloadFunction, function(err, results) {
>   async.map(results, sendFunction, function() {
>   });
>  });
> });
>
> This does not work as expected. Particularly async.map does not work
> as expected, e.g.
>
> function small(char, callback) {
>  callback("error occured", char.toLowerCase());
> };
>
> async.map(["a", "b"], small, function(err, result){
>    console.log(err);
>    console.log(result);
> });
>
> results in:
>
> error occured
> [ 'a' ]
>
>
> which I do not understand. As said, I am completly new to node.
>
> Thank you.
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to