HH\Asio\v

Translate a Traversable of Awaitables into a single Awaitable of Vector

namespace HH\Asio;

function v<Tv>(
  Traversable<Awaitable<Tv>> $awaitables,
): Awaitable<Vector<Tv>>;

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

Returns

Examples

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

<<__EntryPoint>>
async function basic_usage_main(): Awaitable<void> {
  $urls = ImmVector {
    "http://example.com",
    "http://example.net",
    "http://example.org",
  };

  $pages = await get_urls($urls);
  foreach ($pages as $page) {
    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";
}