HH\Asio\vmkw

Returns an Awaitable of Vector of ResultOrExceptionWrapper after a mapping operation has been applied to each key/value pair in the provided KeyedTraversable.

Description

function HH\Asio\vmkw(
  KeyedTraversable<Tk,Tv> $inputs,
  HH\Asio\(function(Tk,Tv):Awaitable<Tr>) $callable,
): Awaitable<Vector<HH\Asio\ResultOrExceptionWrapper<Tr>>>

Returns an Awaitable of Vector of ResultOrExceptionWrapper after a mapping operation has been applied to each key/value pair in the provided KeyedTraversable.

This function is similar to vmk(), except the Vector in the returned Awaitable contains ResultOrExceptionWrappers instead of raw values.

This function is similar to Vector::mapWithKey(), but the mapping of the key/value pairs are done using Awaitables.

This function is called vmkw because we are returning a vector, doing a mapping operation that includes both keys and values, and each member of the Vector is wrapped by a ResultOrExceptionWrapper.

$callable must return an Awaitable.

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

Parameters

  • KeyedTraversable<Tk,Tv> $inputs - The KeyedTraversable of keys and values to map.
  • HH\Asio\(function(Tk,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\vmkw;

// Map a vector of numbers to their value divided by their index
// throwing on division by zero.
$quotients = \HH\Asio\join(\HH\Asio\vmkw(
  Vector { 1, 2, 6, 12 },

  async ($idx, $val) ==> {
    if ($idx != 0) {
      return $val / $idx;
    } else {
      throw new \Exception("Division by zero: " .
        print_r($val, true) . '/' . print_r($idx, true)
      );
    }
  },
));

foreach($quotients as $result) {
  if ($result->isSucceeded()) {
    echo "Success: ";
    var_dump($result->getResult());
  } else {
    echo "Failed: ";
    var_dump($result->getException()->getMessage());
  }
}
Output
Failed: string(21) "Division by zero: 1/0"
Success: int(2)
Success: int(3)
Success: int(4)