This works, but as I have said there are many such methods, so I was wondering if there’s a technique or pattern that I can use to generally do this? One characteristic of such async functions is that the callback parameter is always the last argument of the function. However, what throws me off is that the arguments before the callback can be of any number.

Basically, I want to have a toSuspendFunction function where toSuspendFunction(::functionWithCallback) removes the Callback and make it a suspend function.

I’m guessing I have to use reflections, so any pointers to that (or any other solution) is appreciated!

I personally use something like the awaitCallback<type> { block(it) } in my codebase because I think it looks a little cleaner than wrapping the functions like toSuspendFunction does. Otherwise if you want to use toSuspendFunction you just need to define one for each number of parameters your functions use. I gave you 1 and 2, the others should be easy to implement.

Oh wow, that is beautiful. I didn’t think of that! And yeah, the awaitCallback approach looks cleaner since we don’t have to define one for each argument number. Also +100 for not having to use reflection.