HH\Asio\vfw
Returns an Awaitable
of Vector
of ResultOrExceptionWrapper
after a
filtering operation has been applied to each value in the provided
KeyedTraversable
namespace HH\Asio;
function vfw<Tk, T>(
KeyedTraversable<Tk, T, mixed> $inputs,
(function(T): Awaitable<bool>) $callable,
): Awaitable<Vector<ResultOrExceptionWrapper<T>>>;
This function is similar to vf()
, except the Vector
in the returned
Awaitable
contains ResultOrExceptionWrapper
s instead of raw values.
This function is similar to Vector::filter()
, but the mapping of the values
is done using Awaitable
s.
This function is called vfw
because we are returning a v
ector, doing a
f
iltering operation and each member of the Vector
is w
rapped by a
ResultOrExceptionWrapper
.
$callable
must return an Awaitable
of bool
.
The ResultOrExceptionWrapper
s in the Vector
of the returned Awaitable
are not available until you await
or join
the returned Awaitable
.
Parameters
KeyedTraversable<Tk, T, mixed> $inputs
- TheKeyedTraversable
of values to map.(function(T): Awaitable<bool>) $callable
- The callable containing theAwaitable
operation to apply to$inputs
.
Returns
Awaitable<Vector<ResultOrExceptionWrapper<T>>>
- AnAwaitable
ofVector
ofResultOrExceptionWrapper
after the filtering operation has been applied to the values in$inputs
.
Examples
// Return all non-negative odd numbers
// Positive evens filtered out,
// Negatives and zero cause exception
$odds = \HH\Asio\join(\HH\Asio\vfw(
Vector {-1, 0, 1, 2, 3, 4},
async ($val) ==> {
if ($val <= 0) {
throw new \Exception("$val is non-positive");
} else {
return ($val % 2) == 1;
}
},
));
foreach ($odds as $result) {
if ($result->isSucceeded()) {
echo "Success: ";
var_dump($result->getResult());
} else {
echo "Failed: ";
var_dump($result->getException()->getMessage());
}
}
Failed: string(18) "-1 is non-positive"
Failed: string(17) "0 is non-positive"
Success: int(1)
Success: int(3)