waitAny

Constructs a new task that waits any of the given tasks resolve. The result of the first task to resolve is assimilated by the new task.

This feature is experimental!

This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.

Signature

waitAny(tasks)
forall v, e: ([Task e v Any]) => Task e v Any

Documentation

Constructs a new task that waits any of the given tasks resolve. The result of the first task to resolve is assimilated by the new task.

While the tasks are started in the order they appear in the array, the function will not wait one task to finish before starting another. For asynchronous tasks this effectively gives you concurrent execution.

Note that cancelling the combined task will cancel all of the input tasks, and any of the input tasks resolving will also cancel all of the other input tasks.

Example:

const { task, waitAny } = require('folktale/concurrency/task');

const delay = (ms) => task(
  resolver => {
    const timerId = setTimeout(() => resolver.resolve(ms), ms);
    resolver.cleanup(() => {
      clearTimeout(timerId);
    })
  }
);

const result = await waitAny([
  delay(60),  // cancelled after 30ms
  delay(30)
]).run().promise();

$ASSERT(result == 30);

Properties

Source Code

Defined in source/concurrency/task/wait-any.js at line 16, column 0
(tasks) => {
  if (tasks.length === 0) {
    throw new Error('Task.waitAny() requires a non-empty array of tasks.');
  }

  return tasks.reduce((a, b) => a.or(b));
}
Stability
experimental
Licence
MIT
Module
folktale/concurrency/task/wait-any
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • Quildreen Motta
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)