11

Is it okay to pass parameters, with await? what are the PROS and CONS of doing this?

var results = MapResults(await GetDataAsync());
9
  • 2
    That is fine.All you are doing is resolving the Task before passing the parameter. It is exactly the same as var x = await GetDataAsync; var results = MapResults(x); Makes not difference, and it usually comes down to the coding guidelines you want to implement for yourself
    – Heinrich
    Mar 6, 2020 at 3:17
  • 1
    Code readability. That's literally the same thing as var result = await GetDataAsync(); var map = MapResults(result)
    – Zer0
    Mar 6, 2020 at 3:17
  • @Zer0 which one is readable, the one with var results ? or the single line? Mar 6, 2020 at 3:29
  • @Heinrich yup, I agree with you. I just worry that I am applying some bad practice on my code for doing a single line. Mar 6, 2020 at 3:32
  • 4
    @DrewAguirre: those last two are completely different and YOU MUST NOT DO THE LATTER. I hope that was clear. The former does an asynchronous wait; the latter does a synchronous wait on an asynchronous workflow. Now, suppose the asynchronous workflow has scheduled work in the future on the current thread. A synchronous wait means "sleep this thread until the work that this thread will do in the future is done", and obviously that sleeps forever. Mar 6, 2020 at 19:13

2 Answers 2

33

UPDATE: This question was the subject of my blog in March 2020. See it for more discussion of this issue. Thanks for the interesting question!


I'm going to assume here that you intended that to be a function call as the sole member of the argument list.

As others have noted, there is no difference between

x = M(await FAsync());

and

var f = await FAsync();
x = M(f);

And that is the same as

var ftask = FAsync();
x = M(await ftask)

So it doesn't matter which way you write it, correct?

Give that some thought.


In that specific scenario all three workflows are the same. But there is a potential difference here if we only slightly vary the scenario. Consider:

x = M(await FAsync(), await GAsync());

This is the same as

var f = await FAsync();
var g = await GAsync();
x = M(f, g);

and what do we know about this workflow? The GAsync task is not started until the FAsync task is finished! But it looks like there is an opportunity for having two tasks going at the same time here, which might use the current thread more efficiently! Likely the workflow would be better written as:

var ftask = FAsync();
var gtask = GAsync();
x = M(await ftask, await gtask);

Now FAsync and GAsync tasks both start, and we do not call M until both finish.

My advice is to think carefully about where you put your awaits. Remember, an await is intended to be a point in an asynchronous workflow where the workflow asynchronously pauses until a precondition of the continuation is met. If you can delay awaiting a task until it is actually a precondition, you might be able to eke out a performance win.

2
  • Of course it is good to be aware of the underlying multithreading support of the tasks you're awaiting. You can't await simultaneously on two tasks that yield data from the database and uses the same SqlConnection or DbContext for instance.
    – Sebazzz
    Mar 10, 2020 at 18:28
  • 1
    Wow! I am impressed.. sorry been busy and haven't replied on your answer.. I marked this one as the answer.. explanation is very clear.. and yup, I learned a lot just by reading the few lines you wrote.. and I have read your blog also.. good job! and keep it up.. Apr 6, 2020 at 15:00
5

There is no runtime difference between;

var results = MapResults(await GetDataAsync())

and

var tmp = await GetDataAsync();
var results = MapResults(tmp)
6
  • Yup there is no difference. what I am thinking, is there a technical issue for doing this? or is it cleaner to use "tmp" variable? i can't see any disadvantage for using the first line. even there is an exception it will throw to GetDataAsync(). Mar 6, 2020 at 3:26
  • Is it a bad practice to do the first line ? Mar 6, 2020 at 3:26
  • I prefer second pattern, 1) more read able, 2) I can massage the code before i pass to results.
    – Harris Yer
    Mar 6, 2020 at 3:33
  • Please note that OP used variable (GetDataAsync) and not method call. Depending on where that Task GetDataAsync = CallSomeAsync() is initialized there could be significant differences as task will be running already by the time MapResults(await GetDataAsync) is called. See Eric's answer for more details. Mar 6, 2020 at 5:12
  • Yeah, since there wasn't enough context, I assumed it was a typo.... Mar 6, 2020 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.