async_simple icon indicating copy to clipboard operation
async_simple copied to clipboard

how to adapt to callback style API

Open microcai opened this issue 1 year ago • 3 comments

the referenced document says to use

async_xxx(param, callbackhandler)

to

co_await async_xxx(param);

but, that kind of refactoring only works with asio.

we have some old lib that accepts callbacks. now I want to call that old lib function and "co_await" for the callback to be invoked.

hint: I warpped the Executor for the old lib, so the old lib and async_simple runs in the same Executor. I need a way to convert "lazy<>" to some old style callbacks. and let that callback to resume the calling lazy<>

microcai avatar Jan 31 '24 15:01 microcai

but, that kind of refactoring only works with asio.

This is not true. At least not "only".

we have some old lib that accepts callbacks. now I want to call that old lib function and "co_await" for the callback to be invoked.

I didn't understand where the blocker it. Could you explain your question in detail?

ChuanqiXu9 avatar Feb 01 '24 02:02 ChuanqiXu9

You need to implement awaiter for your asynchronous operation, refer to this demo and coroutine reference

chloro-pn avatar Feb 01 '24 06:02 chloro-pn

And we can use Future/Promise, call promise setValue in callback, and co_await future in Lazy

refer to https://github.com/alibaba/async_simple/blob/main/async_simple/coro/test/FutureAwaiterTest.cpp

RainMark avatar Feb 01 '24 11:02 RainMark