Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the C# equivalent to Promise.all?

I would like to fetch data from multiple locations from Firebase Realtime Database like described here and here by Frank van Puffelen and I can't find any equivalent to Promise.all in c#. What would be the proper way to do it?

like image 428
Dave Avatar asked Feb 02 '19 18:02

Dave


People also ask

What is C language in simple words?

What Does C Programming Language (C) Mean? C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s.

What is C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C for computer?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners.


2 Answers

That you are looking for is Task.WhenAll. You should create as many tasks as the multiple locations from which you want to fetch your data and then feed them in this method.

like image 200
Christos Avatar answered Nov 15 '22 12:11

Christos


To expand on @Christos's accepted answer:

Task.WhenAll appears to be about as close as you will get for a drop-in replacement for Promise.all. I actually found it to be closer than I initially thought. Here's an example using a JavaScript Promise.all implementation that you may want to replicate in C#:

const [ resolvedPromiseOne, resolvedPromiseTwo ] = await Promise.all([ taskOne, taskTwo ]);

In C# you can do something very similar with Task.WhenAll (assuming they return the same types).

var taskList = new[]
{
  SomeTask(),
  AnotherTask()
};

var completedTasks = await Task.WhenAll(taskList);

// then access them like you would any array
var someTask = completedTasks[0];
var anotherTask = completedTasks[1];

// or just iterate over the array
foreach (var task in completedTasks)
{
  doSomething(task);
}

This assumes they're both in async methods / functions.

like image 21
Bryantee Avatar answered Nov 15 '22 14:11

Bryantee