async method inside a controller, webApi

1

I hope you can understand my question. From an Angular application I am consuming a service, the service waits the answer of another URL and depending on that answer certain instructions must be executed but in an ASYNCRONA way, for example if the answer is "true" then 3 instructions are executed asyncrona. I have tried but I get an error that I detail below. ( Note that this is a test example )

  • driver

  • testAsync method

  • Asynchronous method: actionTrue (method action 1 equals action2 and action3)

  • Error that PostMan launches

    
asked by Hersenfels 28.06.2018 в 01:01
source

1 answer

1

You have to keep in mind that if a method is asynchronous, the way to wait for that method to end is using await.

If we see your function testAsync you do not use await for the asynchronous method actionTrue () so the function will return "hello" and will not wait for the action method to finishTrue that's why it gives you the error.

Then you have to do it in the following way:

public async string pruebaAsync (){
   bol result = true;
   if (result){
      await accionTrue();
   }
   return "hola";
}

I hope it works for you and you understand why the error.

    
answered by 28.06.2018 / 11:41
source