HH\Asio\mmw
Returns an Awaitable of Map of ResultOrExceptionWrapper after a
mapping operation has been applied to each value in the provided
KeyedTraversable
namespace HH\Asio;
function mmw<Tk as arraykey, Tv, Tr>(
KeyedTraversable<Tk, Tv> $inputs,
(function(Tv): Awaitable<Tr>) $callable,
): Awaitable<Map<Tk, ResultOrExceptionWrapper<Tr>>>;
This function is similar to mm(), except the Map in the returned
Awaitable contains values of ResultOrExceptionWrapper instead of raw
values.
This function is similar to Map::map(), but the mapping of the values
is done using Awaitables.
This function is called mmw because we are returning a map, doing a
mapping operation and each value member in the Map is wrapped by a
ResultOrExceptionWrapper.
$callable must return an Awaitable.
The ResultOrExceptionWrappers in the Map of the returned Awaitable
are not available until you await or join the returned Awaitable.
Parameters
KeyedTraversable<Tk,Tv> $inputs- TheKeyedTraversableof values to map.(function(Tv): Awaitable<Tr>) $callable- The callable containing theAwaitableoperation to apply to$inputs.
Returns
Awaitable<Map<Tk,ResultOrExceptionWrapper<Tr>>>- AnAwaitableofMapof key/ResultOrExceptionWrapperpairs after the mapping operation has been applied to the values in$inputs.
Examples
<<__EntryPoint>>
async function basic_usage_main(): Awaitable<void> {
// Map a map of numbers to their integer half
// throwing if they can't be divided evenly
$halves = await \HH\Asio\mmw(
Map {
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
},
async ($val) ==> {
if ($val % 2) {
throw new \Exception("$val is an odd number");
} else {
return $val / 2;
}
},
);
foreach ($halves as $num => $result) {
if ($result->isSucceeded()) {
echo "$num / two Success: ";
\var_dump($result->getResult());
} else {
echo "$num / two Failed: ";
\var_dump($result->getException()->getMessage());
}
}
}