Is there an equivalent to JavaScript's Promise.all
in C++ or C++/WinRT for awaitables (or just Windows.Foundation.IAsyncAction
)?
For example, I am trying to gather multiple IAsyncActions and continue when all of them have completed. For now I'm starting each action and then awaiting them one by one in a simple loop:
winrt::IAsyncAction AwaitMultiple(std::vector<winrt::IAsyncAction> actions)
{
for (const auto& action : actions)
{
co_await action;
}
}
// ... somewhere else...
winrt::IAsyncAction MyFunctionAsync()
{
std::vector<winrt::IAsyncAction> actions{};
actions.push_back(/* some function that generates an IAsyncAction */);
actions.push_back(/* some function that generates an IAsyncAction */);
actions.push_back(/* some function that generates an IAsyncAction */);
// ... etc...
co_await AwaitMultiple(actions);
// ... do other stuff...
}
Is there a better way to do this? Is there a language way?
Disclaimer: I work for Microsoft.