N
N
nadonado2018-12-17 12:24:33
C++ / C#
nadonado, 2018-12-17 12:24:33

How to wait for an asynchronous method to complete?

Hello. Example:

public Constructor()
{
    FIrstAsyncMethod();
    int b = 5;
}

async Task FirstAsyncMethod()
{
  await SecondAsyncMethod();
}

async Task SecondAsyncMethod()
{
  ...
}

In this example, the constructor will set variable b to 5 before the FirstAsyncMethod method completes, that is, as soon as an await operator is encountered in the called asynchronous method, the instructions of the method from which the call was made continue to be executed. Question: how to wait for the execution of an asynchronous method to complete and only then assign b = 5 in the constructor? It will not work not to make the FIrstAsyncMethod method asynchronous, there is an api to the database, which has only asynchronous methods for receiving data. Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2018-12-17
@freeExec

public Constructor()
{
    await FIrstAsyncMethod();
    int b = 5;
}

But you don't need to call the asynchronous method in the constructor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question