How can I call an async method inside an anonymous delegate?

n179911

I have a function takes a delegate as input parameter.

public delegate bool Callback();

public static class MyAPI
{
     public static handle(Callback callback) {
           ... 
     }
}

So I call the api with an anonymous delegate like this

MyAPI.handle(delegate
{
    // my implementation
});

My question is how can i call an async method in my anonymous delegate?

MyAPI.handle(delegate
{
    // my implementation
    await MyMethodAsync(...);
});

I get an error saying the 'await' operator can only be used within async anonymous method'?

The function MyAPI.handle() only expect a non async delegate. I can't change that method. How can I fix my problem?

Thank you.

Christos

You could call an asynchronous method by passing an async lambda expression:

MyAPI.handle(async () =>
{
    // my implementation
    await MyMethodAsync(...);
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i call extra method that i define in anonymous class?

From Dev

How can I call the .Invoke method of a Delegate.Target?

From Dev

How to pass async method delegate in async method call?

From Dev

Properly call anonymous method with async

From Dev

How can I call async method from constructor?

From Dev

How can I use a method async Task<string> and return string, and then how do I pass a delegate for that method to a constructor and use it later?

From Dev

How can I call a method at regular intervals inside of a CountDownTimer?

From Dev

How can I call controller/method inside button onclick?

From Dev

android:How can I call setAdapter for ViewPager inside the method onPageSelected?

From Dev

How can I call a class method inside a promise in Angular 2?

From Dev

How can I call a method at regular intervals inside of a CountDownTimer?

From Dev

android:How can I call setAdapter for ViewPager inside the method onPageSelected?

From Dev

Can I call an async method from OnOptionsItemSelected?

From Dev

Can I call a set Method inside a Constructor

From Dev

Can I call $.ajax method inside $.getJson()?

From Dev

How to call async method inside a method which has return type?

From Dev

How can I call asynchronously a method only when other async method finishes working in Swift?

From Dev

iOS : ScrollView delegate method inside of CollectionView not call

From Java

How can I mutate state inside async block with StreamExt::scan method in Rust?

From Dev

How can I call this async method in my Xamarin Forms when my app starts?

From Dev

How can I call this async method in my Xamarin Forms when my app starts?

From Dev

How can I call the class slibing function in javascript anonymous function

From Dev

How can I call anonymous function without using Eval?

From Dev

TypeScript - How can I access "this" in async method

From Dev

How can I define an async method in a trait?

From Dev

How can i make this method async?

From Dev

How to call SuperClass delegate method from SubClass delegate method

From Dev

How can I call a function inside of a function?

From Dev

How can I call a function inside a class?

Related Related

  1. 1

    How can i call extra method that i define in anonymous class?

  2. 2

    How can I call the .Invoke method of a Delegate.Target?

  3. 3

    How to pass async method delegate in async method call?

  4. 4

    Properly call anonymous method with async

  5. 5

    How can I call async method from constructor?

  6. 6

    How can I use a method async Task<string> and return string, and then how do I pass a delegate for that method to a constructor and use it later?

  7. 7

    How can I call a method at regular intervals inside of a CountDownTimer?

  8. 8

    How can I call controller/method inside button onclick?

  9. 9

    android:How can I call setAdapter for ViewPager inside the method onPageSelected?

  10. 10

    How can I call a class method inside a promise in Angular 2?

  11. 11

    How can I call a method at regular intervals inside of a CountDownTimer?

  12. 12

    android:How can I call setAdapter for ViewPager inside the method onPageSelected?

  13. 13

    Can I call an async method from OnOptionsItemSelected?

  14. 14

    Can I call a set Method inside a Constructor

  15. 15

    Can I call $.ajax method inside $.getJson()?

  16. 16

    How to call async method inside a method which has return type?

  17. 17

    How can I call asynchronously a method only when other async method finishes working in Swift?

  18. 18

    iOS : ScrollView delegate method inside of CollectionView not call

  19. 19

    How can I mutate state inside async block with StreamExt::scan method in Rust?

  20. 20

    How can I call this async method in my Xamarin Forms when my app starts?

  21. 21

    How can I call this async method in my Xamarin Forms when my app starts?

  22. 22

    How can I call the class slibing function in javascript anonymous function

  23. 23

    How can I call anonymous function without using Eval?

  24. 24

    TypeScript - How can I access "this" in async method

  25. 25

    How can I define an async method in a trait?

  26. 26

    How can i make this method async?

  27. 27

    How to call SuperClass delegate method from SubClass delegate method

  28. 28

    How can I call a function inside of a function?

  29. 29

    How can I call a function inside a class?

HotTag

Archive