Questions tagged [async-await]

This covers the asynchronous programming model supported by various programming languages, using the async and await keywords.

Several programming languages support an asynchronous programming model using co-routines, with the async and await keywords.

Support for the model was added to

  • C# and VB in VS2012
  • Python in 3.5
  • ECMAScript in ECMAScript 2017
  • Rust in 1.39
  • C++ in C++20
  • Swift in Swift 5.5

C# and Visual Studio

Asynchronous programming with async and await was introduced with C# 5.0 in Visual Studio 2012. The run-time support for this language concept is a part of .NET 4.5 / Windows Phone 8 / Windows 8.x Store Runtime.

It's also possible to use async/await and target .NET 4.0 / Windows Phone 7.1 / Silverlight 4 / MonoTouch / MonoDroid / Portable Class Libraries, with Visual Studio 2012+ and Microsoft.Bcl.Async NuGet package, which is licensed for production code.

Async CTP add-on for VS2010 SP1 is also available, but it is not suitable for product development.

Python

Similar syntax was introduced to Python 3.5 (see PEP 492 - Coroutines with async and await syntax.

Previously, it was possible to write co-routines using generators; with the introduction of await and async co-routines were lifted to a native language feature.

C++

Coroutines is introduced in C++20. Using the co_await operator results in suspended execution until resumed. Values can be returned using co_yield and co_return keywords which correspond to suspending and completing execution, respectively.

Swift

The async-await pattern was introduced in Swift 5.5 at WWDC 2021, as part of a broader Swift concurrency initiative. Historically, asynchronous patterns were achieved through the use of Grand Central Dispatch (GCD, ) and “completion handler closure” patterns. The Swift concurrency aims to provide a more intuitive asynchronous coding environment.

ECMAScript

The async and await keywords were first reserved in the ECMAScript 2016 specification and then their use and behavior was fully-specified in the ECMAScript 2017 specification.

Historically, ECMAScript offered “promises”, an improvement over traditional callback patterns, where this sort of looping and exception handling is challenging. Task.js and similar libraries further refined promises, to further simplify the process. But with async functions, all the remaining boilerplate is removed, leaving only the semantically meaningful code in the program text.

Asynchronous vs multi-threaded

The async-await pattern simplifies the writing of asynchronous code. While it is frequently used in multi-threaded environments, it should be noted that “asynchronous” and “multi-threaded” represent two different concepts. The async and await keywords merely allow us to represent relationships and dependencies between asynchronous tasks in a more natural manner, which may be distinct from the mechanism to run code on a different thread. The async-await pattern offers great utility in a multi-threaded environment, but it is not, itself, the multi-threaded mechanism.

Resources:

C#

C++

Swift

Related:

26583 questions
136
votes
9 answers

Intellij Idea warning - "Promise returned is ignored" with aysnc/await

I'm using Express.js in my code with Node.js v7.3. In this I've created a User Router which forwards the requests to my User Controller. I'm using async/await inside the User Controller to do asynchronous calls. The problem is that IntelliJ gives me…
Jyotman Singh
  • 10,792
  • 8
  • 39
  • 55
136
votes
3 answers

Entity Framework Queryable async

I'm working on some some Web API stuff using Entity Framework 6 and one of my controller methods is a "Get All" that expects to receive the contents of a table from my database as IQueryable. In my repository I'm wondering if there is any…
Jesse Carter
  • 20,062
  • 7
  • 64
  • 101
136
votes
4 answers

Struggling trying to get cookie out of response with HttpClient in .net 4.5

I've got the following code that works successfully. I can't figure out how to get the cookie out of the response. My goal is that I want to be able to set cookies in the request and get cookies out of the response. Thoughts? private async…
Peter Kellner
  • 14,748
  • 25
  • 102
  • 188
135
votes
5 answers

What is the difference between JavaScript promises and async await?

I have been using ECMAScript 6 and ECMAScript 7 features already (thanks to Babel) in my applications - both mobile and web. The first step obviously was to ECMAScript 6 levels. I learnt many async patterns, the promises (which are really…
bozzmob
  • 12,364
  • 16
  • 50
  • 73
133
votes
6 answers

Correct Try...Catch Syntax Using Async/Await

I like the flatness of the new Async/Await feature available in Typescript, etc. However, I'm not sure I like the fact that I have to declare the variable I'm awaiting on the outside of a try...catch block in order to use it later. Like so: let…
freedomflyer
  • 2,431
  • 3
  • 26
  • 38
132
votes
3 answers

Is async HttpClient from .Net 4.5 a bad choice for intensive load applications?

I recently created a simple application for testing the HTTP call throughput that can be generated in an asynchronous manner vs a classical multithreaded approach. The application is a able to perform a predefined number of HTTP calls and at the end…
Florin Dumitrescu
  • 8,182
  • 4
  • 33
  • 29
131
votes
2 answers

Await on a completed task same as task.Result?

I'm currently reading "Concurrency in C# Cookbook" by Stephen Cleary, and I noticed the following technique: var completedTask = await Task.WhenAny(downloadTask, timeoutTask); if (completedTask == timeoutTask) return null; return await…
julio.g
  • 3,268
  • 5
  • 28
  • 25
129
votes
10 answers

ASP.NET Web API OperationCanceledException when browser cancels the request

When a user loads a page, it makes one or more ajax requests, which hit ASP.NET Web API 2 controllers. If the user navigates to another page, before these ajax requests complete, the requests are canceled by the browser. Our ELMAH HttpModule then…
129
votes
3 answers

Wrapping synchronous code into asynchronous call

I have a method in ASP.NET application, that consumes quite a lot of time to complete. A call to this method might occur up to 3 times during one user request, depending on the cache state and parameters that user provides. Each call takes about 1-2…
Eadel
  • 3,797
  • 6
  • 38
  • 43
129
votes
4 answers

Get TransactionScope to work with async / await

I'm trying to integrate async/await into our service bus. I implemented a SingleThreadSynchronizationContext based on this example http://blogs.msdn.com/b/pfxteam/archive/2012/01/20/10259049.aspx. And it works fine, except for one thing:…
Yann
  • 1,388
  • 2
  • 11
  • 9
128
votes
5 answers

An async/await example that causes a deadlock

I came across some best practices for asynchronous programming using c#'s async/await keywords (I'm new to c# 5.0). One of the advices given was the following: Stability: Know your synchronization contexts ... Some synchronization contexts are…
dror
  • 3,759
  • 6
  • 31
  • 45
126
votes
4 answers

Using await outside of an async function

I was attempting to chain two async functions together, because the first had a conditional return parameter that caused the second to either run, or exit the module. However, I've found odd behavior I can't find in the specs. async function…
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
125
votes
4 answers

Async function without await in JavaScript

I have two functions, a and b, that are asynchronous, the former without await and the latter with await. They both log something to the console and return undefined. After calling either of the function, I log another message and look if the…
Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
125
votes
7 answers

How to use ES8 async/await with streams?

In https://stackoverflow.com/a/18658613/779159 is an example of how to calculate the md5 of a file using the built-in crypto library and streams. var fs = require('fs'); var crypto = require('crypto'); // the file you want to get the hash var…
user779159
  • 9,034
  • 14
  • 59
  • 89
125
votes
4 answers

Why shouldn't all functions be async by default?

The async-await pattern of .net 4.5 is paradigm changing. It's almost too good to be true. I've been porting some IO-heavy code to async-await because blocking is a thing of the past. Quite a few people are comparing async-await to a zombie…
talkol
  • 12,564
  • 11
  • 54
  • 64