I have a method that takes a Action<String>
. When the method finishes its processing it calls the Action<String>
with the return value.
MethodWithCallback((finalResponse)=> {
Console.WriteLine(finalResponse);
});
I want to use this in a web.api async controller. How do I wrap this method so I can await
for this method to complete in an async
manner. I cannot modify the method itself, it is in a legacy code base.
What I would like to be able to do is this
String returnValue = await MyWrapperMethodThatCallsMethodWithCallback();
The await keyword is used in an async function to ensure that all promises returned in the async function are synchronized, ie. they wait for each other. Await eliminates the use of callbacks in . then() and .
Async functions not only allow the programmer to escape from callback hell and promise chaining in asynchronous code, but they also make the code seemingly synchronous.
The await keyword, by contrast, is non-blocking, which means the current thread is free to do other things during the wait.
You can leverage the TaskCompletionSource class and solve the problem in a generic way:
Task<T> AsAsync<T>(Action<Action<T>> target) {
var tcs = new TaskCompletionSource<T>();
try {
target(t => tcs.SetResult(t));
} catch (Exception ex) {
tcs.SetException(ex);
}
return tcs.Task;
}
That way you don't have to modify your MethodWhitCallback
:
var result = await AsAsync<string>(MethodWithCallback);
Console.WriteLine(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With