0

I have two methods that are similar, however, after theorizing about this, I'm pretty sure they are different in execution.

MethodOne:

        var renderDocumentDirective = await 
        RenderDocumentBuilder.Instance.GetRenderDocumentDirectiveAsync(previousPage, session);

        return await ResponseClient.Instance.BuildAlexaResponse(new Response()
        {
            shouldEndSession = null,
            directives = new List<IDirective>()
            {
                renderDocumentDirective
            }

        }, session.alexaSessionDisplayType);

MethodTwo

            var renderDocumentDirective = RenderDocumentBuilder.Instance.GetRenderDocumentDirectiveAsync(previousPage, session);
            return await ResponseClient.Instance.BuildAlexaResponse(new Response()
            {
                shouldEndSession = null,
                directives = new List<IDirective>()
                {
                    await renderDocumentDirective
                }

            }, session.alexaSessionDisplayType);

The first method uses the await operator on the async task, RenderDocumentBuilder, prior to its uses inside the ResponseClient, which is also an async task.

However the second method, sets up the Task RenderDocumentBuilder, but doesn't call the awaited method until it is inside the ResponseClient, which, at this point in execution, is waiting to return data.

Both ways of executing this method work, but I am unclear if it is proper to:

  • await a task outside the ResponseClient? (method 1)

  • Or, is it proper to create the renderDocumentDirective Task outside the ResponseClient and await it inside the method? (method 2)

5
  • 2
    I would argue that first one is cleaner and readable but unless new Response() calls have some strange side effects they are pretty much the same. Commented Sep 28, 2020 at 14:39
  • 1
    Would be interesting to see what IL they compile down to; I suspect the same thing. I'd echo Guru Stron in that the first one is more readable and the intention is clearer. Also, I think you can better handle any exceptions arising from GetRenderDocumentDirectiveAsync in the first example. Commented Sep 28, 2020 at 14:43
  • 2
    @GuruStron Why would the implementation of ResponseClient.Instance matter? The entire expression is evaluated before awaiting anyway. Commented Sep 28, 2020 at 14:43
  • 1
    @JohnathanBarclay yep, agreed. Commented Sep 28, 2020 at 14:44
  • Do you mean it is evaluated before awaiting because it is a singleton instance? Commented Sep 28, 2020 at 17:31

1 Answer 1

2

I think you misinterpret the flow of data.

Here are your two methods written a bit more verbose

Method 1

var renderTask = RenderDocumentBuilder.Instance.GetRenderDocumentDirectiveAsync(previousPage, session);

var renderDocumentDirective = await renderTask;

var alexaResponse = new Response();

alexaResponse.shouldEndSession = null,
alexaResponse.directives = new List<IDirective>();
alexaResponse.directives.Add(renderDocumentDirective);

var buildTask = ResponseClient.Instance.BuildAlexaResponse(alexaResponse, session.alexaSessionDisplayType);

return await buildTask;

Method 2

var renderTask = RenderDocumentBuilder.Instance.GetRenderDocumentDirectiveAsync(previousPage, session);

var alexaResponse = new Response()
alexaResponse.shouldEndSession = null,
alexaResponse.directives = new List<IDirective>();
alexaResponse.directives.Add(await renderTask);

var buildTask = ResponseClient.Instance.BuildAlexaResponse(alexaResponse, session.alexaSessionDisplayType);

return await buildTask;

So you see that the only real difference is that methode 2 creates the Response object, sets shouldEndSession and creates the List object before or it awaits the renderTask.

Method 2 might be beneficial, but this depends on how GetRenderDocumentDirectiveAsync is implemented (i.e. truely async). But even if it is, it is highly unlikly that method 2 brings any performance gains as there is not much difference between both methods.

That said, I would go with method 1, because it looks more like sync code and in most cases you want to await a Task as soon you have it available, because await/async is mainly about freeing threads to do other stuff and not about parallalism.

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

Comments

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.