HH\Asio\mfw

Returns an Awaitable of Map of ResultOrExceptionWrapper after a filtering operation has been applied to each value in the provided KeyedTraversable.

Description

function HH\Asio\mfw(
  KeyedTraversable<Tk,T> $inputs,
  HH\Asio\(function(T):Awaitable<bool>) $callable,
): Awaitable<Map<Tk,HH\Asio\ResultOrExceptionWrapper<T>>>

Returns an Awaitable of Map of ResultOrExceptionWrapper after a filtering operation has been applied to each value in the provided KeyedTraversable.

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

This function is similar to Map::filter(), but the filtering of the values is done using Awaitables.

This function is called mfw because we are returning a map, doing a filtering operation and each value member in the Map is wrapped by a ResultOrExceptionWrapper.

$callable must return an Awaitable of bool.

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

Parameters

  • KeyedTraversable<Tk,T> $inputs - The KeyedTraversable of values to fitler.
  • HH\Asio\(function(T):Awaitable<bool>) $callable - The callable containing the Awaitable operation to apply to $inputs.

Return Values

Examples

<?hh

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

// Return all non-negative odd numbers
// Positive evens filtered out,
// Negatives and zero cause exception
$odds = \HH\Asio\join(\HH\Asio\mfw(
  Map {
    '-one' => -1,
    'zero' => 0,
    'one' => 1,
    'two' => 2,
    'three' => 3,
    'four' => 4,
  },

  async ($val) ==> {
    if ($val <= 0) {
      throw new \Exception("$val is non-positive");
    } else {
      return ($val % 2) == 1;
    }
  },
));

foreach($odds as $num => $result) {
  if ($result->isSucceeded()) {
    echo "$num Success: ";
    var_dump($result->getResult());
  } else {
    echo "$num Failed: ";
    var_dump($result->getException()->getMessage());
  }
}
Output
-one Failed: string(18) "-1 is non-positive"
zero Failed: string(17) "0 is non-positive"
one Success: int(1)
three Success: int(3)