2

one simple question. I found some methods with this "logic" and "architecture".

public async Task<T> FindAsync(params object[] keys)
{
    return await this.context.FindAsync(keys);
}

One single instruction with its await. Since the method is async you have to do this (otherwise compiler errors occur). IMHO i can't find why you should use this pattern because if the method is async you probably want to perform different tasks in parallel. If you sync the execution with the await keyword you make the method close to sync and you loose all the performance gain of the managed thread pool mechanism of .net. What is your opinion? I'm in wrong?

2
  • You are not required to use await; you have to remove both async and await keywords, in that case. Probably the only benefit afforded here is that this FindAsync method will appear in call stacks if there is an exception. Commented Aug 6, 2016 at 6:42
  • 1
    await doesn't make code synchronous. async/await don't have anything to do with the thread pool. You may find my async intro helpful. Commented Aug 8, 2016 at 15:33

1 Answer 1

5

If you're calling this method like this:

await FindAsync(); // this method waits for the task to complete

Then it doesn't make any sense to return await inside this method, you can just change it to:

public Task<T> FindAsync(params object[] keys)
{
    return this.context.FindAsync(keys); // Start and return the task
}

Then the caller awaits the task to Complete.

Sign up to request clarification or add additional context in comments.

1 Comment

I agree with you. The caller of async methods should await all async methods in order to optimize and run some parts of code (async methods) in parallel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.