I have a function that returns an IAsyncEnumerable. I want to await it for completion, but I do not care about any of the results. I only care about the side effects of calling the function.
How do can I easily await the IAsyncEnumerable for completion without using a for loop or turning it into a container (e.g. ToListAsync)?
 
                        
You could use the
LastOrDefaultAsyncoperator from the System.Linq.Async package, and then convert the resultingValueTask<T>to aTaskby using theAsTaskextension method. For extra convenience you could combine these two operations to one extension methodToTask:But honestly doing a loop manually is simpler and probably more efficient:
The
LastOrDefaultAsyncoperator will preserve in memory the last emitted element during the enumerable's lifetime, which may prevent the element from being garbage collected in a timely manner. You could prevent this from happening by including in the query theIgnoreElementsoperator from the System.Interactive.Async package, but I think it becomes too much of a hassle.