ExpressionEvaluator icon indicating copy to clipboard operation
ExpressionEvaluator copied to clipboard

[suggestion] Implement async / await

Open lofcz opened this issue 4 years ago • 3 comments

~~Currently this is not included in todo / roadmap document here~~ - https://github.com/codingseb/ExpressionEvaluator/wiki/ExpressionEvaluator-Todo-List

Implementing async / await (with priority on await) would greatly increase flexibility of EE. C# is becoming more and more asynchronous with each new version and by using async patterns we can free current thread while external work is being processed (querying a database, creating/saving a stream, waiting for an external library to do some work...). This is critical for web applications where threadpool is very limited and we need to free our threads as often as possible.

This suggestion hence proposes that async / await keywords would be recognized when parsing scripts.

Backend:

public class Main {
     public staic void Main() {
          ExpressionEvaluator eval = new();
          eval .StaticTypesForExtensionsMethods.Add(typeof(MyClassExt));
          eval.Variables = new Dictionary<string, object> {
               { "SomeAsyncMethod", new Func<int>(async () => await someAwaitableJobReturningInt())} }
          }

          eval.ScriptEvaluate(script);
     }
}

Script:

myResult = await SomeAsyncMethod();

Note that we would also need to support this on extension methods.

For the sake of supporting various syntaxes keywords should be remappable.

myResult = waitfor SomeAsyncMethod(); /* waitfor = await */

lofcz avatar Jan 14 '21 11:01 lofcz

Yes it would be great. I need some reflection on how to do it. I keep it open as a todo 😃

codingseb avatar Jan 14 '21 14:01 codingseb

Core of the problem here is that we need to "await" EventHandler which we can't do as it returns void. Something like DefferedEvents would be needed here - https://pedrolamas.github.io/DeferredEvents/

what do you think?

lofcz avatar Jan 14 '21 18:01 lofcz

+1 to this feature

israellot avatar Jul 30 '21 19:07 israellot