Skip to content

async.race = execute in parallel, but only use the first result #568

@rprieto

Description

@rprieto

I found myself needing this type of function a few times:

  • execute several async operations in parallel
  • call done whenever the first one finished, and ignore any subsequent results

It's easily implemented as follows, but does it belong in async? Happy to submit a PR with unit tests.

async.first = function(ops, done) {
  var i = 0;
  var response = false;
  for (i = 0; i < ops.length; ++i) {
    ops[i](function(err, data) {
      if (!response) {
        response = true;
        done(err, data);
      }
    });
  }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions