HH\Asio\vw
Translate a Traversable
of Awaitables
into a single Awaitable
of
Vector
of ResultOrExceptionWrapper
namespace HH\Asio;
function vw<Tv>(
Traversable<Awaitable<Tv>> $awaitables,
): Awaitable<Vector<ResultOrExceptionWrapper<Tv>>>;
This function is the same as v()
, but wraps the results into
ResultOrExceptionWrapper
s.
This function takes any Traversable
object of Awaitables
(i.e., each
member of the Traversable
is of type of Awaitable
, likely from a call
to a function that returned Awaitable<T>
), and transforms those
Awaitables
into one big Awaitable
Vector
of ResultOrExceptionWrapper
.
This function is called vw
because we are returning a v
ector of
Awaitable
w
rapped into ResultofExceptionWrapper
s.
The ResultOrExceptionWrapper
s in the Vector
of the returned Awaitable
are not available until you await
or join
the returned Awaitable
.
Parameters
Traversable<Awaitable<Tv>> $awaitables
- The collection ofTraversable
awaitables.
Returns
Awaitable<Vector<ResultOrExceptionWrapper<Tv>>>
- AnAwaitable
ofVector
ofResultOrExceptionWrapper
, where theVector
was generated from eachTraversable
member in$awaitables
.
Examples
async function one(): Awaitable<int> {
return 1;
}
$mcr = \MCRouter::createSimple(ImmVector {});
$handles = \HH\Asio\vw(Vector {
// This will throw an exception, since there's no servers to speak to
$mcr->get("no-such-key"),
// While this will obviously succeed
one(),
});
$results = \HH\Asio\join($handles);
foreach ($results as $result) {
if ($result->isSucceeded()) {
echo "Success: ";
var_dump($result->getResult());
} else {
echo "Failed: ";
var_dump($result->getException()->getMessage());
}
}
Failed: string(38) "get failed with result mc_res_notfound"
Success: int(1)