HH\Asio\mmkw
Returns an Awaitable
of Map
of ResultOrExceptionWrapper
after a
mapping operation has been applied to each key/value pair in the provided
KeyedTraversable
namespace HH\Asio;
function mmkw<Tk as arraykey, Tv, Tr>(
KeyedTraversable<Tk, Tv> $inputs,
(function(Tk, Tv): Awaitable<Tr>) $callable,
): Awaitable<Map<Tk, ResultOrExceptionWrapper<Tr>>>;
This function is similar to mmk()
, except the Map
in the returned
Awaitable
contains values of ResultOrExceptionWrapper
instead of raw
values.
This function is similar to Map::mapWithKey()
, but the mapping of the keys
and values is done using Awaitable
s.
This function is called mmkw
because we are returning a m
ap, doing a
m
apping operation on k
eys and values, and each value member in the Map
is w
rapped by a ResultOrExceptionWrapper
.
$callable
must return an Awaitable
.
The ResultOrExceptionWrapper
s in the Map
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<Map<Tk,
ResultOrExceptionWrapper<Tr>>>
- AnAwaitable
ofMap
of key/ResultOrExceptionWrapper
pairs after the mapping operation has been applied to the keys an 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\mmkw(
Map {
1 => 1,
0 => 2,
2 => 6,
3 => 12,
},
async ($div, $val) ==> {
if ($div != 0) {
return $val / $div;
} else {
throw new \Exception(
"Division by zero: ".\print_r($val, true).'/'.\print_r($div, true),
);
}
},
);
foreach ($quotients as $result) {
if ($result->isSucceeded()) {
echo " Success: ";
\var_dump($result->getResult());
} else {
echo "Failed: ";
\var_dump($result->getException()->getMessage());
}
}
}