Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8864475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:14:59+00:00 2026-06-14T16:14:59+00:00

How would I go about writing an async controller action that is made up

  • 0

How would I go about writing an async controller action that is made up of 1 +n async calls (await)? E.g. let’s pretend I need to retrieve the object Foo first and it has a variable number of Bars identifier and I need to fetch all those entities (for the sake of this question, there is no fetchBarsByFooId. Would I use Task.WaitAll or maybe Parallel.For?

public async Task<ActionResult> Bars(int id) {
    Foo foo = await this.FooProvider.GetFooAsync(id);
    var bars = new ConcurrentQueue<Bar>();

    // Sub-optimal version
    foreach (int barId in foo.BarIDs) {
        Bar bar = this.BarProvider.GetBar(barId);
        bars.Enqueue(bar)
    }

    // Fetch each bar asynchronously and pass them to the view.
    ....

    return View(bars.ToArray());
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:15:01+00:00Added an answer on June 14, 2026 at 4:15 pm

    Well, the first thing to work out is whether you actually want parallelism, or just asynchrony. Just asynchrony is simple:

    var bars = new List<Bar>();
    foreach (var id in foo.Bars)
    {
        bars.Add(await BarProvider.GetBarAsync(id));
    }
    return View(bars);
    

    For parallelism, you could use Task.WhenAll:

    var tasks = foo.Bars.Select(id => BarProvider.GetBarAsync(id));
    var bars = await Task.WhenAll(tasks);
    return View(bars);
    

    (I realize this is shorter code than the non-parallel version – but it’s more conceptually tricky.)

    You should not use Parallel.For or Task.WaitAll as both of those are blocking calls – whereas Task.WhenAll returns you a task which will complete when all the subtasks have completed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would i go about writing a function that would handle the data in
How would one go about writing a query that selects items 2000-2010 out of
How would I go about writing a Makefile that would call, say, gedit hashtable.cpp
How would you go about writing a file from emacs that contains only the
How would I go about writing an Android application that will go online, search
How would i go about writing a .bat file, so that i can drag
Given a username, how would I go about writing an LDAP query that will
How would I go about writing python functions that can be append to strings
How would go about writing proper unit testing (and integration testing for that matter)
How you would go about writing a script that calculates the percent change between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.