HH\Asio\vmw
Returns an Awaitable
of Vector
of ResultOrExceptionWrapper
after a
mapping operation has been applied to each value in the provided
Traversable
namespace HH\Asio;
function vmw<Tv, Tr>(
Traversable<Tv> $inputs,
(function(Tv): Awaitable<Tr>) $callable,
): Awaitable<Vector<ResultOrExceptionWrapper<Tr>>>;
This function is similar to vm()
, except the Vector
in the returned
Awaitable
contains ResultOrExceptionWrapper
s instead of raw values.
This function is similar to Vector::map()
, but the mapping of the values
is done using Awaitable
s.
This function is called vmw
because we are returning a v
ector, doing a
m
apping operation and each member of the Vector
is w
rapped by a
ResultOrExceptionWrapper
.
$callable
must return an Awaitable
.
The ResultOrExceptionWrapper
s in the Vector
of the returned Awaitable
are not available until you await
or join
the returned Awaitable
.
Parameters
Traversable<Tv>
$inputs
- TheTraversable
of values to map.(function(Tv): Awaitable<Tr>) $callable
- The callable containing theAwaitable
operation to apply to$inputs
.
Returns
Awaitable<Vector<ResultOrExceptionWrapper<Tr>>>
- AnAwaitable
ofVector
ofResultOrExceptionWrapper
after the mapping operation has been applied to the values in$inputs
.
Examples
<<__EntryPoint>>
async function basic_usage_main(): Awaitable<void> {
// Map a vector of numbers to half integer half
// throwing if they can't be divided evenly
$halves = await \HH\Asio\vmw(
Vector {1, 2, 3, 4},
async ($val) ==> {
if ($val % 2) {
throw new \Exception("$val is an odd number");
} else {
return $val / 2;
}
},
);
foreach ($halves as $result) {
if ($result->isSucceeded()) {
echo "Success: ";
\var_dump($result->getResult());
} else {
echo "Failed: ";
\var_dump($result->getException()->getMessage());
}
}
}