【问题标题】:Asynchronous hierarchy异步层次结构
【发布时间】:2022-02-02 01:05:23
【问题描述】:

如果我有一个异步方法,其参数中包含一个异步方法和一个非异步方法。

哪个方法会先运行?

例子:

await ExampleMethod(
            Func<Task>: await Example.SomeMethod(), 
            Func<IEnumerable<T>> Example.SomeOtherMethod()
            );

【问题讨论】:

  • 你测试的时候发生了什么?
  • 它在本地运行良好,但在实时环境中却不行
  • “很好”?这是什么意思?
  • 在 SomeMethod 中首先在本地运行,但在实时环境中它似乎没有

标签: asynchronous async-await


【解决方案1】:

哪个方法会先运行?

异步方法调用没有什么神奇之处。他们start executing synchronously, just like any other method(正如我在博客中描述的那样)。此外,异步根本不会影响参数评估的顺序。

所以,这段代码:

await ExampleMethod(await Example.SomeMethod(), Example.SomeOtherMethod());

与这段代码基本相同:

var parameter1 = await Example.SomeMethod();
var parameter2 = Example.SomeOtherMethod();
await ExampleMethod(parameter1, parameter2);

【讨论】:

  • 感谢斯蒂芬,这对我帮助很大。感谢您更深入地了解它
猜你喜欢
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
相关资源
最近更新 更多