1

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?

3
  • 4
    Do you have an example of something in C# which isn't working as expected? async methods return Tasks, which can be stored in variables, passed around, and ultimately awaited. What have you tried and what didn't work as expected? Commented Feb 4, 2022 at 20:28
  • Start here: Asynchronous programming with async and await Commented Feb 4, 2022 at 20:57
  • Are you searching for the TaskCompletionSource class? Commented Feb 5, 2022 at 5:01

2 Answers 2

2

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.

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

Comments

1

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 :

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

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.