HH\Asio\vmkw
Returns an Awaitable
of Vector
of ResultOrExceptionWrapper
after a
mapping operation has been applied to each key/value pair in the provided
KeyedTraversable
namespace HH\Asio;
function vmkw<Tk, Tv, Tr>(
KeyedTraversable<Tk, Tv> $inputs,
(function(Tk, Tv): Awaitable<Tr>) $callable,
): Awaitable<Vector<ResultOrExceptionWrapper<Tr>>>;
This function is similar to vmk()
, except the Vector
in the returned
Awaitable
contains ResultOrExceptionWrapper
s instead of raw values.
This function is similar to Vector::mapWithKey()
, but the mapping of the
key/value pairs are done using Awaitable
s.
This function is called vmkw
because we are returning a v
ector, doing a
m
apping operation that includes both k
eys and values, 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
KeyedTraversable<Tk,
Tv> $inputs
- TheKeyedTraversable
of keys and values to map.(function(Tk, 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 keys and values in$inputs
.
Examples
<<__EntryPoint>>
async function basic_usage_main(): Awaitable<void> {
// Map a vector of numbers to their value divided by their index
// throwing on division by zero.
$quotients = await \HH\Asio\vmkw(
Vector {1, 2, 6, 12},
async ($idx, $val) ==> {
if ($idx != 0) {
return $val / $idx;
} else {
throw new \Exception(
"Division by zero: ".\print_r($val, true).'/'.\print_r($idx, true),
);
}
},
);
foreach ($quotients as $result) {
if ($result->isSucceeded()) {
echo "Success: ";
\var_dump($result->getResult());
} else {
echo "Failed: ";
\var_dump($result->getException()->getMessage());
}
}
}