async all the way down issue

前端 未结 2 1597
面向向阳花
面向向阳花 2021-01-18 13:43

I have an async asp.net controller. This controller calls an async method. The method that actually performs the async IO work is deep down in my application. The series of

2条回答
  •  情歌与酒
    2021-01-18 14:20

    Yes. There is a penalty (though not a huge one), and if you don't need to be async don't be. This pattern is often called "return await" where you can almost always remove both the async and the await. Simply return the task you already have that represents the asynchronous operations:

    private Task C_Async(int id)
    {
        // This method executes very fast
        var idTemp = paddID(id);
        return D_Async(idTemp);
    }
    
    private Task D_Async(string id)
    {
        // This method executes very fast
        return E_Async(id);
    }
    

    In this specific case Index will only await the tasks that E_Async returns. That means that after all the I/O is done the next line of code will directly be return View();. C_Async and D_Async already ran and finished in the synchronous call.

提交回复
热议问题