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
10
votes
3 answers

should we choose async await over Promise in Javascript

I know that the async await is the new Promise in the town and it is a new way to write asynchronous code and I also know that We didn’t have to write .then, create an anonymous function to handle the response Async/await makes it finally possible…
Vikas Bansal
  • 10,662
  • 14
  • 58
  • 100
10
votes
3 answers

Fetch API returning old data in react native

Description: I'm making a react native app, where I have a list of github users that I'm following and I want to implement the functionality to unfollow and refresh the list. I made two asynchronous helpers to interact with the github API, one to…
ygongdev
  • 264
  • 3
  • 19
10
votes
2 answers

Typescript async/await not working

I've a problem about async/await on typescript with target es2017. Below is my code : my route.ts : method: 'POST', config: { auth: { strategy: 'token', } }, path:…
kola
  • 269
  • 2
  • 5
  • 11
10
votes
3 answers

Paradoxical issue with mocha done() and async await

I have the following test case: it("should pass the test", async function (done) { await asyncFunction(); true.should.eq(true); done(); }); Running it asserts: Error: Resolution method is overspecified. Specify a…
Alon
  • 10,381
  • 23
  • 88
  • 152
10
votes
2 answers

Can I detect async/await available in browser?

As title, how can I detect async/await es7 support in browser? Is that possible?
3142 maple
  • 865
  • 2
  • 11
  • 27
10
votes
3 answers

Javascript wait for condition to be true using promises

I am struggling to make a simple waiting function in my program. I want to use promises and async await if possible. What I have so far: function waitForCondition(conditionObj) { var start_time = new Date().getTime() function checkFlag() { …
T Mack
  • 950
  • 3
  • 12
  • 27
10
votes
4 answers

How is async/await working in serial and parallel?

I have two async functions. Both of them are waiting for two 3 seconds function calls. But the second one is faster than the first. I think the faster one is running in parallel and other in serial. Is my assumption correct? If yes, why is this…
Vadakkumpadath
  • 1,445
  • 13
  • 21
10
votes
2 answers

Does await await promise-like objects?

According to Mozilla, await only awaits Promises: [rv] Returns the resolved value of the promise, or the value itself if it's not a Promise. If you await a non-Promise, a resolved promise will be immediately returned, and it will not await.…
wezten
  • 2,126
  • 3
  • 25
  • 48
10
votes
2 answers

Async/Await nodejs support?

Just a small problem that I can't fix. I'm on Node v8.1.1 and I try to use async/await but it doesn't work. My code snippet looks like this : const axios = require('axios'); const TOKEN = '...'; const httpClient = axios.create({ baseURL :…
mbreton
  • 674
  • 1
  • 6
  • 19
10
votes
1 answer

asyncio as_yielded from async generators

I'm looking to be able to yield from a number of async coroutines. Asyncio's as_completed is kind of close to what I'm looking for (i.e. I want any of the coroutines to be able to yield at any time back to the caller and then continue), but that…
freebie
  • 2,161
  • 2
  • 19
  • 36
10
votes
2 answers

Race condition in JavaScript with compound assignment

I'm not talking about complex race conditions involving the network or events. Rather, I seem to have found out that the += operator is not atomic in V8 (Chrome 58, or Node 8). The code below aims to run two so-called threads in parallel. Each…
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
10
votes
1 answer

how can I access to a variable in try block in other try block?

http://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/ In this link, there are some codes that access to a variable in try catch but when I try this in my server It doesn't work because It's out of scope. how can I…
Phillip YS
  • 784
  • 3
  • 10
  • 33
10
votes
1 answer

C# Mysql - Using of locks on database query for async await server

I have TcpListener class and I'm using async/await reading and writing. For this server I have created single database instance where I have prepared all database queries. But for more then one TcpClient I'm keep getting exception: An exception of…
Erik Šťastný
  • 1,487
  • 1
  • 15
  • 41
10
votes
2 answers

Error handling in a Promise.all like flow in async/await syntax

I have some issues to handle multiple rejections in "parallel". How to handle rejection in a async function when we "await in parallel". Here an example : function in_2_sec(number) { return new Promise((resolve, reject) => { …
Smirow
  • 125
  • 2
  • 8
10
votes
1 answer

What is async in Python?

I've read about new Python "keywords" async and await. However they are neither really keywords nor reserved in a namespace. >>> import keyword >>> keyword.iskeyword("async") False >>> async Traceback (most recent call last): File "", line…
Thyrst'
  • 2,253
  • 2
  • 22
  • 27
1 2 3
99
100