question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: How to use with async / await?

See original GitHub issue

Question: is there a way to call Match with async / await? Related to #26

I have:

public async Task<OneOf<Foo, None>> HandleAsync(GetFooQuery query)
{
	var foo = await GetByIdAsync(query.FooId);

	return foo.Match<OneOf<Foo, None>>(
		f => GetCustomerNameForFooAsync(f).Result,
		_ => new None());
}

private async Task<OneOf<Foo, None>> GetByIdAsync(FooId fooId)
{ ...

private async Task<Foo> GetCustomerNameForFooAsync(Foo foo)
{ ...

but the .Result on the end is a bit icky (edit: and is not asyncronous!). Would be nice if I could do something like:

public async Task<OneOf<Foo, None>> HandleAsync(GetFooQuery query)
{
	var foo = await GetByIdAsync(query.FooId);

	return foo.Match<OneOf<Foo, None>>(
		async f => await GetCustomerNameForFooAsync(f),
		_ => new None());
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mcintyre321commented, Mar 28, 2019

Hi Matt

I think the cleanest way to write it with the current lib is if you get liberal with async:

async Task<OneOf<Foo, None>> HandleAsync(GetFooQuery query)
{
	var foo = await GetByIdAsync(query.FooId);

	return await foo.Match<Task<OneOf<Foo, None>>>(
		async f => await GetCustomerNameForFooAsync(f),
		async _ => new None()); //you'll get a warning about it not being async, but ignore it!
}
0reactions
mattfrearcommented, Nov 1, 2022

Thanks for the reply Harry.

Yep, that works, it’s the same suggestion you made last time. My bad - I missed your comment //you'll get a warning about it not being async, but ignore it.

I’m pedantic about not having compiler warnings so I stuck with the ugly Task.FromResult and then edited my message to get rid of the question altogether.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interview question: async & await (C#)
Q : What is the purpose of async / await keywords? These keywords allow writing asynchronous non-blocking code in a synchronous fashion.
Read more >
C# Async/Await Interview Questions And Answers (2023)
This comprehensive article covers a wide range of C# async-await interview questions that will test your understanding of the async-await ...
Read more >
Javascript Async/Await: Javascript Interview questions #10
Hello everyone, in this tutorial we will learn about how to use async functions, awaiting async functions, and the importance of async /...
Read more >
20 Async/Await Interview Questions and Answers - CLIMB
Async /Await Interview Questions and Answers · 1. Can you explain what asynchronous programming is? · 2. How does async/await help with performance ......
Read more >
Top 25 Async Await Interview Questions and Answers
Discover essential Async Await interview questions and answers for JavaScript developers in our comprehensive guide, designed to help you confidently ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found