Snippets

cellfish await or not await

Created by cellfish last modified
void Main()
{
	TestIt(true).Wait();
	"**********************************".Dump();
	TestIt(false).Wait();
}

async Task TestIt(bool bar)
{
	try
	{	        
		if (bar)
			await BarAsync(TimeSpan.FromSeconds(1));
		else
			await BazAsync(TimeSpan.FromSeconds(1));
	}
	catch (InvalidOperationException ex)
	{
		ex.StackTrace.Dump();
	}
}

async Task FooAsync(TimeSpan t)
{
	await Task.Delay(t);
	throw new InvalidOperationException();
}

async Task BarAsync(TimeSpan t)
{
	await FooAsync(t);
}

Task BazAsync(TimeSpan t)
{
	return FooAsync(t);
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.