HH\Asio\v

Translate a Traversable of Awaitables into a single Awaitable of Vector.

Description

function HH\Asio\v(
  Traversable<Awaitable<Tv>> $awaitables,
): Awaitable<Vector<Tv>>

Translate a Traversable of Awaitables into a single Awaitable of Vector.

This function takes any Traversable object of Awaitables (i.e., each member of the Traversable is of type of Awaitable, likely from a call to a function that returned Awaitable<T>), and transforms those Awaitables into one big Awaitable Vector.

This function is called v we are returning a vector of Awaitable.

Only When you await or join the resulting Awaitable, will all of the values in the Vector within the returned Awaitable be available.

Parameters

  • Traversable<Awaitable<Tv>> $awaitables - The collection of Traversable awaitables.

Return Values

Examples

<?hh

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

/**
 * Query an arbitrary number of URLs in parallel
 * returning them as a Vector of string responses.
 */
async function get_urls(
  \ConstVector<string> $urls,
): Awaitable<Vector<string>> {
  // Wrap each URL string into a curl_exec awaitable
  $handles = $urls->map($url ==> \HH\Asio\curl_exec($url));

  // Wait on each handle in parallel and return the results
  return await \HH\Asio\v($handles);
}

$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>