template <typename E, typename F, typename...Ts>
auto when_any(E e, F f, future<Ts>... args)
(1)
template <typename E, typename F, typename I> 
auto when_any(E e, F f, const std::pair<I,I>& range)
(2)
  1. This function create a future that continues whenever the first future of the passed arguments args succeeds.

  2. This function create a future that continues whenever the first future within the range succeeds

Parameters

e

Executor which is used to schedule the resulting task

f

Callable object that implements the task

args

Futures that shall be joined

range

This describes the range of futures. If an empty range is provided then a stlab::future_exception with code stlab::future_errc::broken_promise is thrown.

Return value

a future that reduces a number of input futures to the first that successds and passes it to the associated function object

Example 1

#include <iostream>
#include <string>
#include <thread>
#include <stlab/concurrency/default_executor.hpp>
#include <stlab/concurrency/future.hpp>

using namespace std;
using namespace stlab;

int main() {
    auto argument1 = async(default_executor, [] { return 42; });
    auto argument2 = async(default_executor, [] { return 815; });

    auto result = when_any(default_executor, [](int x, std::size_t index) {
        cout << "The current result is " << x << " " << index << '\n';
    }, argument1, argument2);

    // Waiting just for illustrational purpose
    while (!result.get_try()) { this_thread::sleep_for(chrono::milliseconds(1)); }
}

/*
    Result:

        The current result is 42 0
        or 
        The current result is 815 1
*/