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

<<__EntryPoint>>
async function basic_usage_main(): Awaitable<void> {
  // Map a vector of numbers to their value divided by their index
  // throwing on division by zero.
  $quotients = await \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());
    }
  }
}