Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
How to control C# Task (async/await in same way as javascript Promise)?

I'm js developer and jumped to C#. I'm learning async/await/Task in C# and can't understand how to control C# Task.

In javascript I can save Promise resolve handler to call it later (and resolve promise), for example, to organize communication between some "badly connected" app parts (request doesn't returns result, but result is sent separately and fires some "replyEvent"), and do something like this:

// javascript
const handlers = {}

// event firing on reply
const replyEventListener = (requestId, result) => {
  if (handlers[requestId]) {
    handlers[requestId](result)
    delete handlers[requestId]
  }
}

const requestFunction = (id, params) => new Promise(resolve => {
  // handler is saved to use later from "outside"
  handlers[id] = resolve;
  // do required request
  makeRequest(params)
})

// .. somewhere in code request becomes simple awaitable
const resultOfRequest = await requestFunction(someUniqueId)

How can I do same with C# Task?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think the philosophy behind Promise (in JS) and Task (in C#) is different. We don't implement them exactly the same way.

Read the documentation on the MSDN :

  • Task Class - Remarks

  • Task<TResult> Class

You must empty your cup to be able to fill it again.

about 4 years ago · Juan Pablo Isaza Report

0

A good article in MSDN is the following which shows examples of how Tasks can be used like Promises in the sense that you can start them and then await them later.

Asynchronous programming with async and await

If you want to await a Task in C# like you would a Promise in JS, you would do the following:

  1. Create a method or function that returns a Task. Usually this will be a Task of some class object. In the article above, it references creating breakfast as an analogy, so you can define FryEggsAsync as a method that takes in an int number of how many eggs to fry and returns a Task<Egg>. The C# convention is to end any function or method name with Async to indicate that a Task is being returned.
  2. Create your Task, which is similar to a Promise in JS, so you could do var eggsTask = FryEggsAsync(2), which would store a Task<Egg> in eggsTask, that you can then await or pass to other functions as needed.
  3. To await your Task to resolve it and get the result, you would simply do await eggsTask.

You can also use Task.WaitAll to await multiple Tasks at once, similar to Promise.all in JS, by passing in an array of Tasks as an argument to that method call. See the Task.WaitAll documentation for some examples of this. The difference here though is that the results are stored separately in each task passed to Task.WaitAll, instead of aggregated together into a results array like in JS with Promise.all, but if you need to await multiple Tasks then this will be an easier way to do so.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!