HH\Asio\mmkw

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

namespace HH\Asio;

function mmkw<Tk as arraykey, Tv, Tr>(
  KeyedTraversable<Tk, Tv> $inputs,
  (function(Tk, Tv): Awaitable<Tr>) $callable,
): Awaitable<Map<Tk, ResultOrExceptionWrapper<Tr>>>;

This function is similar to mmk(), except the Map in the returned Awaitable contains values of ResultOrExceptionWrapper instead of raw values.

This function is similar to Map::mapWithKey(), but the mapping of the keys and values is done using Awaitables.

This function is called mmkw because we are returning a map, doing a mapping operation on keys and values, and each value member in the Map is wrapped by a ResultOrExceptionWrapper.

$callable must return an Awaitable.

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

Parameters

Returns

Examples

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

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

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