Task and Lazy syntactic sugar #1383
Replies: 6 comments
-
I think that making each What you're proposing would make the code more confusing, and it would also make it ambiguous: Though there is one thing you could do to make your code simpler: make To do that, you can use the following extension method: public static TaskAwaiter<T> GetAwaiter<T>(this Lazy<Task<T>> lazyTask) =>
lazyTask.Value.GetAwaiter(); |
Beta Was this translation helpful? Give feedback.
-
Another thing, you can change this- async Task<T> MethodAsync() => await AnotherMethodAsync(param1, param2); to Task<T> MethodAsync() => AnotherMethodAsync(param1, param2); Whenever I see |
Beta Was this translation helpful? Give feedback.
-
Similar to #827 (in particular chaining |
Beta Was this translation helpful? Give feedback.
-
@gulshan Just don't ever do that if it's inside a |
Beta Was this translation helpful? Give feedback.
-
@gulshan Doing that removes stack frames from exceptions, making it harder to debug. In the example below, Just something to keep in mind - it's not neccesarily bad, but there is a tradeoff between performance and diagnostics. using System;
using System.Linq;
using System.Threading.Tasks;
namespace stacktrace
{
public class Program
{
public static void Main(string[] args)
{
try { RunAsync(true).Wait(); } catch (Exception ex) { Console.WriteLine(ex); }
try { RunAsync(false).Wait(); } catch (Exception ex) { Console.WriteLine(ex); }
}
static async Task RunAsync(bool shouldAwait)
{
if (shouldAwait)
{
await Foo();
}
else
{
await Bar();
}
}
static async Task Foo() => await BlowUp(true);
static Task Bar() => BlowUp(false);
static async Task BlowUp(bool value)
{
await Task.Yield();
throw new InvalidOperationException("aaaaaaah");
}
}
} |
Beta Was this translation helpful? Give feedback.
-
I considered saying exactly what @yaakov-h has put very well. IIRC, Stephen Cleary recommends not trying to elide the async and await in most cases even where possible. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions