Call multiple async methods that rely on each other

后端 未结 1 1851
[愿得一人]
[愿得一人] 2021-01-07 14:24

I\'m looking for best practice around calling multiple async methods where each next method relies on the values returned from one before.

I\'m experimenting with 2

相关标签:
1条回答
  • 2021-01-07 15:12

    I'm looking for best practice around calling multiple async methods where each next method relies on the values returned from one before.

    A lot of asynchronous questions can be answered by looking at the synchronous equivalent. If all the methods are synchronous and each method depends on the results of previous methods, how would that look?

    var T1 = Sum(2,5);
    var T2 = Sum(T1, 7);
    var T3 = Sum(T2, 7);
    

    Then the asynchronous equivalent would be:

    var T1 = await SumAsync(2,5);
    var T2 = await SumAsync(T1, 7);
    var T3 = await SumAsync(T2, 7);
    

    P.S. For future reference, do not insert StartNew or Task.Run as generic placeholders for asynchronous code; they just confuse the issue since they have very specific use cases. Use await Task.Delay instead; it's the Thread.Sleep of the async world.

    0 讨论(0)
提交回复
热议问题