HH\Asio\vm

Returns an Awaitable of Vector containing after a mapping operation has been applied to each value in the provided Traversable.

Description

function HH\Asio\vm(
  Traversable<Tv> $inputs,
  HH\Asio\(function(Tv):Awaitable<Tr>) $callable,
): Awaitable<Vector<Tr>>

Returns an Awaitable of Vector containing after a mapping operation has been applied to each value in the provided Traversable.

This function is similar to Vector::map(), but the mapping of the values is done using Awaitables.

This function is called vm because we are returning a vector, and we are doing a mapping operation.

$callable must return an Awaitable.

The values in the Vector of the returned Awaitable are not available until you await or join the returned Awaitable.

Parameters

  • Traversable<Tv> $inputs - The Traversable of values to map.
  • HH\Asio\(function(Tv):Awaitable<Tr>) $callable - The callable containing the Awaitable operation to apply to $inputs.

Return Values

Examples

<?hh

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

/**
 * Query an arbitrary number of URLs in parallel
 * returning them as a Vector of string responses.
 *
 * Refer to \HH\Asio\v() for a more verbose version of this.
 */
async function get_urls(
  \ConstVector<string> $urls,
): Awaitable<Vector<string>> {

  // Invoke \HH\Asio\curl_exec for each URL,
  // then await on each in parallel
  return await \HH\Asio\vm(
    $urls,
    fun("\HH\Asio\curl_exec"),
  );
}

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

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