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
11
votes
4 answers

The return type of an async function must be the global Promise type

Why does TSlint still says "The return type of an async function or method must be the global Promise type"? I can't understand what's wrong. UPDATED:
11
votes
1 answer

Angular 6 Async-await not working on http request

Hi im using angular 6 to call a rest api with the code below. I am trying to acheive making the code synchronous with the async-await function. however something is missing async save() { if (this.changedRecords.length !== 0) { …
Joshua Vandenbor
  • 509
  • 3
  • 6
  • 19
11
votes
1 answer

asyncio: collecting results from an async function in an executor

I would like to start a large number of HTTP requests and collect their results, once all of them have returned. Sending the requests in a non-blocking fashion is possible with asyncio, but I have problems collecting their results. I'm aware of…
lhk
  • 27,458
  • 30
  • 122
  • 201
11
votes
1 answer

How can I make child_process.spawn return a Promise in my TypeScript module?

I'm trying to write a little module that uses child_process.spawn to clone a git repo and return a Promise but it fails on me. When I'm using spawnSync it works. Here is the sync code that works. import {spawnSync} from 'child_process'; export…
fabianmoronzirfas
  • 4,091
  • 4
  • 24
  • 41
11
votes
4 answers

Python - How to - Big Query asynchronous tasks

This may be a dummy question but I cannot seem to be able to run python google-clood-bigquery asynchronously. My goal is to run multiple queries concurrently and wait for all to finish in an asyncio.wait() query gatherer. I'm using…
Antoine Dussarps
  • 469
  • 1
  • 5
  • 17
11
votes
4 answers

NamedPipeServerStream.ReadAsync() does not exit when CancellationToken requests cancellation

When the NamedPipeServer stream reads any data from the pipe it does not react to CancellationTokenSource.Cancel() Why is that? How can I limit the time I'm waiting in the server for data from the client? Code to reproduce: static void Main(string[]…
inwenis
  • 368
  • 7
  • 24
11
votes
1 answer

Python - Overloading asynchronous methods

I'm building a wrapper for an API. For now, it works. However, it is synchronous, using the requests module for HTTP calls. I would like to implement a way to asynchronously call these methods while not having to use a different name or lib…
Plutoberth
  • 709
  • 7
  • 19
11
votes
2 answers

Async getter/setter in Sequelize as part of a property

Can I define a getter of a property as an asyc function in Sequelize? In the getter I should retrieve a value from another table and I've tried this in the model definition: ... bio: { type: Sequelize.STRING, get: async function() { …
lellefood
  • 1,885
  • 3
  • 18
  • 40
11
votes
5 answers

Performance Metrics/Diagnostics of .NET Tasks

Is there a way to get data out from .NET about the number of (C# 5 or newer, so post-async/await) Tasks pending execution, and similar metrics, for use in diagnosing issues happening to production servers? The case I'm discussing is an…
Mike Roibu
  • 307
  • 2
  • 13
11
votes
2 answers

how to do async await on a forEach function

I'm a beginner in async await and promises. I read few articles and watch few tutorial videos but I am still not able to understand it completely. So I have a code that I'm working on right now }).then(function() { var responseArray = [] …
anoop chandran
  • 1,460
  • 5
  • 23
  • 42
11
votes
1 answer

Awaiting a Callback method

I'm calling a third-party API which has a method that looks like this: myServiceClient.Discover(key, OnCompletionCallback); public bool OnCompletionCallback(string response) { // my code } My challenge is, I have to call Discover because it…
Some User
  • 5,257
  • 13
  • 51
  • 93
11
votes
3 answers

Unhandled promise rejection on AWS lambda with async / await

Recently AWS announced the availability of the nodejs8.10 runtime for their lambda functions (Node.js 8.10 runtime available). While this seemed great for the happy flow, I'm running into some problems with the unhappy flow, i.e. I'm getting…
CodeVision
  • 111
  • 1
  • 1
  • 4
11
votes
1 answer

ASP.NET MVC (Async) CurrentCulture is not shared between Controller and View

I have an ASP.NET MVC 4 application that is targeting .NET Framework 4.7.1, with the problem that the culture is not shared between Controller and View, if the action contains async calls. I am referencing the NuGet package Microsoft.AspNet.Mvc…
11
votes
4 answers

Angular data binding won't work with async/await, yet it will with promises

Data bindings don't get updated if their values are changed after an await statement. handle() { this.message = 'Works' } async handle() { this.message = 'Works' } async handle() { await new Promise((resolve, reject) => { …
ovg
  • 1,486
  • 1
  • 18
  • 30
11
votes
2 answers

Why doesn't this deadlock in ASP.NET Core Web API?

I read Stephen Cleary's post Don't Block on Async Code, so I created an ASP.NET Core Web API project: class AsyncTest { public async Task DoSomethingAsync() { await Task.Delay(3000); return "I'm back"; …
cateyes
  • 5,208
  • 2
  • 24
  • 31