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
12
votes
1 answer

Is it okay to not await async method call?

I have an application which will upload files. I don't want my application to halt during the file upload, so I want to do the task asynchronously. I have something like this: class Program { static void Main(string[] args) { //less…
Bruno Klein
  • 3,217
  • 5
  • 29
  • 39
12
votes
2 answers

C# async tasks waiting indefinitely

I am trying to use the functionality provided by "async" & "await" to asynchronously download webpage content and I have into issues where the Tasks are waiting forever to complete. Could you please let me know what is wrong with the following code…
infinity
  • 1,900
  • 4
  • 29
  • 48
12
votes
1 answer

What's wrong with consuming ConfiguredTaskAwaitable from PortableClassLibrary's class under Debugger from MSTest Runner or Console App?

Note: This is only a Debug-time error, but a very weird one. Problem: While running with Debugger attached and calling a method, exposed in separate Portable library, returning ConfiguredTaskAwaitable, we get InvalidProgramException. Reproduce…
12
votes
2 answers

Tasks vs. TPL Dataflow vs. Async/Await, which to use when?

I have read through quite a number technical documents either by some of the Microsoft team, or other authors detailing functionality of the new TPL Dataflow library, async/await concurrency frameworks and TPL. However, I have not really come across…
Matt
  • 7,004
  • 11
  • 71
  • 117
12
votes
1 answer

Multiplexing C# 5.0's async over a thread pool -- thread safe?

This may seem a little crazy, but it's an approach I'm considering as part of a larger library, if I can be reasonably certain that it's not going to cause weird behavior. The approach: Run async user code with a SynchronizationContext that…
ShZ
  • 6,518
  • 1
  • 26
  • 22
12
votes
1 answer

When should i use async/await and when not?

Should i use async/await from now on (c# 5) everytime when i don't require the outcome of an method immediatelly (Task<>) or i have to fire a one-off method (void)? Should i use it in all the cases when i used the Task class in C# 4 and pushed work…
rudimenter
  • 3,242
  • 4
  • 33
  • 46
12
votes
2 answers

Can/should Task be wrapped in a C# 5.0 awaitable which is covariant in TResult?

I'm really enjoying working with C# 5.0 asynchronous programming. However, there are a few places where updating old code to be consistent with the TAP model is causing problems for me. Here's one of them - I'm not sure exactly why Task is…
lightw8
  • 3,262
  • 2
  • 25
  • 35
12
votes
2 answers

Why does this async / await code generate "...not all code paths return a value"?

Hopefully this isn't a repeat, but there are 5000+ questions here with "not all code paths return a value"! Quite simply, why does this method with a non-generic implementation compile just fine: public static async Task TimeoutAfter(this Task…
HolySamosa
  • 9,011
  • 14
  • 69
  • 102
11
votes
1 answer

Prefetching and yielding to "hide" cache misses in rust

When C++20 got stackless coroutines some papers successfully used them to "hide" cache misses via prefetching and switching to another coroutine. As far as I can tell rust's async is also like stackless coroutines in that it is a "zero cost…
fakedrake
  • 6,528
  • 8
  • 41
  • 64
11
votes
3 answers

How to properly cancel Swift async/await function

I have watched Explore structured concurrency in Swift video and other relevant videos / articles / books I was able to find (swift by Sundell, hacking with swift, Ray Renderlich), but all examples there are very trivial - async functions usually…
11
votes
1 answer

Why does my async method builder have to be a class or run in Debug mode?

I'm trying to implement my own async method builder for a custom awaitable type. My awaitable type is just a struct containing a ValueTask. The problem is my asynchronous method builder only works when it's a class or compiled in Debug mode, not…
Matt Thomas
  • 5,279
  • 4
  • 27
  • 59
11
votes
2 answers

How to execute async functions in VSCode debugger?

If I drop into the VSCode debugger in some javascript code and call an asynchronous function with await it just returns a promise. How can I resolve the promise within the debugger so I can see what the result is? For example, if I define a function…
Alex Long
  • 261
  • 2
  • 5
11
votes
2 answers

Async Stack Traces in Node.js 14.15.0

Based on the documentation I would assume that Node.js 14 does now support stack traces in async code but unfortunately using node --async-stack-traces test.js still generates only a partial stack trace and I would be most interested to understand…
doberkofler
  • 9,511
  • 18
  • 74
  • 126
11
votes
3 answers

what's the difference of calling a normal function from async function with await a coroutine from an async function?

async def caller(): await bar() print("finish") async def bar(): // some code here async def caller(): bar() print("finish") def bar(): //some code here In above example. caller has to wait for the completion of bar()…
shakalaka
  • 151
  • 1
  • 7
11
votes
1 answer

Argument of type '(dispatch: Dispatch) => void' is not assignable to parameter of type 'AnyAction'

The error itself: (alias) deleteCategory(id: number): (dispatch: Dispatch) => void import deleteCategory Argument of type '(dispatch: Dispatch) => void' is not assignable to parameter of type 'AnyAction'. Property 'type' is missing in…
Adil Akhmetov
  • 121
  • 1
  • 1
  • 7