in Education by
I have questions regarding the execution order of async jobs. I will ask my question with example because it is easier to be understandable. It is an official example from https://msdn.microsoft.com/en-us/library/mt674882.aspx with some twist. async Task AccessTheWebAsync() { HttpClient client = new HttpClient(); //async operation: Task getStringTask = client.GetStringAsync("http://msdn.microsoft.com"); // You can do work here that doesn't rely on the string from GetStringAsync. DoWork1(); // The await operator suspends AccessTheWebAsync. string urlContents = await getStringTask; DoWork2(); return urlContents.Length; } Can I say DoWork2 is a callback of client.GetStringAsync? If so, DoWork2 is not immediately executed following the completion of client.GetStringAsync IF DoWork1 runs longer time than client.GetStringAsync. Am I right here? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
DoWork2 is not immediately executed following the completion of client.GetStringAsync if DoWork1 runs longer time than client.GetStringAsync Once you hit that point await client.GetStrinkAsync, DoWork1() has already completed, as from your example it looks as it's execution is synchronous. Once client.GetStringAsync completes, then DoWork2 is set to execute. The execution flow will be: GetStringAsync asynchronously starts. It is executed until it hits it's first internal await, and then yields control back to AccessTheWebAsync. DoWork1() kicks off. As it's synchronous, everyone waits for it's completion client.GetStringAsync is asynchronously waited for completion. await will naturally yield the control of execution to it's caller. Once client.GetStringAsync completes, execution will return to AccessTheWebAsync and DoWork2() will execute synchronously. urlContents.length will be returned. Generally, everything after the first await keyword is set to be the continuation for the rest of the method.

Related questions

0 votes
    I have questions regarding the execution order of async jobs. I will ask my question with example because ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    Why do we use Async and Await in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    I need to react in a main process to random events happening in a child process. I have implemented ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    Which refers to a property of computer to run several operation simultaneously and possible as computers await ... Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    There are a lot of samples on running Async/Await functions. But only with one await call. I don' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 11, 2022 in Education by JackTerrance
0 votes
    I copied and pasted a for-await-of example from MDN and still get an error telling me await is a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I copied and pasted a for-await-of example from MDN and still get an error telling me await is a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Which is refers to a stalemate situation due to which no further progress is possible as computer await ... Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    URL is http://*.*.*.*/100/?id=1&version=1 params is {"cityId": "110000", "query": {" ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: asynchronous python itertools chain multiple generators (2 answers) Closed 2 ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have been implementing a [] attribute for some of our larger .NET solutions that will allow configurable analytics to be ... |]).Invoke(async, [|other; f|]) args.ReturnValue...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to make a simple useQuery request but the variables which I am dependent on for the request are ... data } = useQuery(GET_USER_DATA, { variables: userVariables, //...
asked Apr 2, 2022 in Education by JackTerrance
...