Naios / continuable

C++14 asynchronous allocation aware futures (supporting then, exception handling, coroutines and connections)
https://naios.github.io/continuable/
MIT License
815 stars 44 forks source link

Hi, How to span one coroutine ? #25

Closed IronsDu closed 4 years ago

IronsDu commented 4 years ago

@Naios


Commit Hash

{Please write here}

Expected Behavior

{Please write here}

Actual Behavior

{Please write here}

Steps to Reproduce

{Please write here}

Your Environment

IronsDu commented 4 years ago

Straight start one async operation?And use co_await in it's lambda ?Thank you.

Naios commented 4 years ago

Hey, if I understood you right, you can do something like the following:

  ([]() -> continuable<> {
    co_await async([] {
      // ..
    });

    co_return;
  }()).then([] { /* ... */ });

If that doesn't help you, try to explain your question more in detail please, since it's difficult for me to get the actual question out of your two sentences. Maybe you could provide an example what you are trying to accomplish, so I can understand your actual use case better.

IronsDu commented 4 years ago

Thank you for reply me. my questions is :

  1. How to write the async parameter lambda ?
  2. I want simple convert my async call to sync call. for example:, look here : https://github.com/IronsDu/gaylord/blob/master/temp_dep/redis_client/RedisClient.h#L195. (it use ananas::Promise). Ofcause, I prefer want use coroutine, it's better more.
  3. I compiler continuable failed in VS 2019. make_range_looper function error:https://github.com/Naios/continuable/blob/fa589a1e9506dc96b3f511791625c7a03b2c2252/include/continuable/detail/operations/loop.hpp#L166
Naios commented 4 years ago

Probably it is better for you to retrieve the promise callback from continuable as following:

continuable<> do_something_with_redis(std::string some_param) {
  return make_continuable<void>([some_param] (auto&& promise) mutable {
    WaitReplyCallback::Ptr wrapperCallback = std::make_shared<WaitReplyCallback>();
    wrapperCallback->callback = [promise = std::forward<decltype(promise)>(promise)]() mutable {
      promise.set_value();
    };
  });
}

// And then work with the do_something_with_redis like the following
continuable<> do_sth() {
  co_await do_something_with_redis("param");

  co_await do_something_else();

  co_return;
}

For further guidance please take a look at the documentation.

Because of the VS 2019 build issue please open another issue with a reproducable example that yields the error you mentioned above.

Naios commented 4 years ago

Since there was no further activity on this issue I'm marking it as resolved. If you encounter any new bugs or questions open a new issue please.