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
56
votes
5 answers

When should I use ConfigureAwait(true)?

Has anyone come across a scenario for using ConfigureAwait(true)? Since true is the default option I cannot see when would you ever use it.
NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61
56
votes
2 answers

How does await async work in C#

I am trying to understand how await async work in C# and one thing is confusing me a lot. I understand that any method that uses await keyword must be marked with async. My understanding is that when a line with await keyword is hit the code below…
Afraz Ali
  • 2,672
  • 2
  • 27
  • 47
56
votes
5 answers

An entry point cannot be marked with the 'async' modifier

I copied below code from this link.But when I am compiling this code I am getting an entry point cannot be marked with the 'async' modifier. How can I make this code compilable? class Program { static async void Main(string[] args) { …
user2408588
55
votes
10 answers

awaitable Task based queue

I'm wondering if there exists an implementation/wrapper for ConcurrentQueue, similar to BlockingCollection where taking from the collection does not block, but is instead asynchronous and will cause an async await until an item is placed in the…
spender
  • 117,338
  • 33
  • 229
  • 351
55
votes
2 answers

How to await multiple Promises?

I have following code, fileStatsPromises is of Promise[], both foo and bar are Promise[]. What is the correct way to await them? I want to get []. const files = await readDir(currentDir); const fileStatsPromises =…
Zen
  • 5,065
  • 8
  • 29
  • 49
55
votes
2 answers

How to use async/await in Python 3.5?

#!/usr/bin/env python3 # -*- coding: utf-8 -*- import time async def foo(): await time.sleep(1) foo() I couldn't make this dead simple example to run: RuntimeWarning: coroutine 'foo' was never awaited foo()
smilingpoplar
  • 1,065
  • 1
  • 15
  • 26
55
votes
2 answers

Await for list of Tasks

I'm trying to do something like this: foreach (var o in ObjectList) { CalculateIfNeedToMakeTaskForO(o); if (yes) TaskList.Add(OTaskAsync()); } Now I would like to wait for all these tasks to complete. Besides doing foreach(var…
ctlaltdefeat
  • 778
  • 1
  • 6
  • 12
55
votes
1 answer

Interfaces and async methods

I have an application. This application uses an interface to access the database. This interface can be implemented by many classes. For example, one uses EF 4.4, but other classes can use EF5 that is more efficient. In the future perhaps I will…
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
54
votes
2 answers

How can I send an HTTP request from my FastAPI app to another site (API)?

I am trying to send 100 requests at a time to a server http://httpbin.org/uuid using the following code snippet from fastapi import FastAPI from time import sleep from time import time import requests import asyncio app = FastAPI() URL=…
john mich
  • 2,477
  • 3
  • 17
  • 32
54
votes
4 answers

'await' has no effect on the type of this expression

I searched about this but I didn't find anything specific for what I need. If there is one, please, share here. I'm trying to create a generic service to be called in various components. Since it's a function that requests data from an external…
Raphael Alvarenga
  • 868
  • 1
  • 7
  • 13
54
votes
8 answers

Flutter multiple async methods for parrallel execution

I'm still struggeling with the async/await pattern so I'm here to ask you some precisions. I saw this page explaining the async/await pattern pretty well. I'm posting here the example that bother me : import 'dart:async'; Future
Maxouille
  • 2,729
  • 2
  • 19
  • 42
54
votes
2 answers

Python: what are the advantages of async over threads?

I've had a hard time trying to understand how and why async functionality works in python and I am still not sure I understand everything correctly (especially the 'why' part). Please correct me if I am wrong. The purpose of both async methods and…
lesnik
  • 2,507
  • 2
  • 25
  • 24
54
votes
2 answers

Await inside for loop is admitted in Dart?

I have a program like the following: main() async { ooClass = new OoClass(1); int val = await function1(); print(val); ooClass = new OoClass(2); val = await function1(); print(val); ooClass = new OoClass(3); val = await function1(); …
J F
  • 1,058
  • 2
  • 10
  • 21
54
votes
3 answers

@asyncio.coroutine vs async def

With the asyncio library I've seen, @asyncio.coroutine def function(): ... and async def function(): ... used interchangeably. Is there any functional difference between the two?
Jason
  • 2,278
  • 2
  • 17
  • 25
54
votes
4 answers

Running async methods in parallel

I've got an async method, GetExpensiveThing(), which performs some expensive I/O work. This is how I am using it: // Serial execution public async Task> GetThings() { var first = await GetExpensiveThing(); var second = await…
Dave New
  • 38,496
  • 59
  • 215
  • 394