HH\Asio\mm

Returns an Awaitable of Map containing after a mapping operation has been applied to each value in the provided KeyedTraversable

Description

namespace HH\Asio;

function mm<Tk, 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

Return Values

Examples

<?hh

namespace Hack\UserDocumentation\API\Examples\HH\Asio\mm;

/**
 * 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"),
  );
}

$urls = ImmMap {
  'com' => "http://example.com",
  'net' => "http://example.net",
  'org' => "http://example.org",
};

$pages = \HH\Asio\join(get_urls($urls));
foreach ($pages as $name => $page) {
  echo $name, ': ';
  echo substr($page, 0, 15) . ' ... ' . substr($page, -8);
}
Output
com: <!doctype html> ... </html>
net: <!doctype html> ... </html>
org: <!doctype html> ... </html>