HH\Asio\mm
Returns an Awaitable of Map containing after a mapping operation has
been applied to each value in the provided KeyedTraversable
namespace HH\Asio;
function mm<Tk as arraykey, Tv, Tr>(
KeyedTraversable<Tk, Tv> $inputs,
(function(Tv): Awaitable<Tr>) $callable,
): Awaitable<Map<Tk, Tr>>;
This function is similar to Map::map(), but the mapping of the values
is done using Awaitables.
This function is called mm because we are returning a map, and doing a
mapping operation.
$callable must return an Awaitable.
The keys and values 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,Tr>>- AnAwaitableofMapafter the mapping operation has been applied to the values in$inputs.
Examples
/**
* Query an arbitrary number of URLs in parallel
* returning them as a Map of string responses.
*
* Refer to \HH\Asio\m() for a more verbose version of this.
*/
async function get_urls(
\ConstMap<string, string> $urls,
): Awaitable<Map<string, string>> {
// Invoke \HH\Asio\curl_exec for each URL,
// then await on each in parallel
return await \HH\Asio\mm($urls, fun("\HH\Asio\curl_exec"));
}
<<__EntryPoint>>
async function basic_usage_main(): Awaitable<void> {
$urls = ImmMap {
'com' => "http://example.com",
'net' => "http://example.net",
'org' => "http://example.org",
};
$pages = await get_urls($urls);
foreach ($pages as $name => $page) {
echo $name, ': ';
echo \substr($page, 0, 15).' ... '.\substr($page, -8);
}
}
```.skipif
// Skip if we don't have an internet connection
if (!\get_headers("www.example.com")) {
print "skip";
}