1

I want to dispatch a job to the UI thread, then wait for the result and use it from another thread. Like this, but co_await does not work inside the lambda:

dispatcher.RunAsync(CoreDispatcherPriority::Normal, [&]() {
    auto res = co_await mStoreContext.RequestPurchaseAsync(L"");
});

Or even waiting for the whole RunAsync operation, if I could get my result out from it

1
  • 1
    The more idiomatic way would be co_await resume_foreground(dispatcher); auto res = co_await mStoreContext.RequestPurchaseAsync(L""); co_await resume_background(); use_result(res); Commented Jun 1, 2019 at 14:35

1 Answer 1

1

That's because void can not be used as a return value from a coroutine (I can be if you use my my library).

try returning a std::future<void> instead:

dispatcher.RunAsync(CoreDispatcherPriority::Normal, [&]() -> std::future<void> {
    auto res = co_await mStoreContext.RequestPurchaseAsync(L"");
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. even better would be a way to get the result out of the complete runasync block, but this works fine
This is dangerous because of the reference capture. The caller cannot return until it is sure the lambda has finished running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.