Questions tagged [asynchronous]

Asynchronous programming is a strategy for deferring operations with high latency or low priority, usually in an attempt to improve performance, responsiveness, and / or composability of software. Such strategies are usually employed using some combination of event-driven programming and callbacks, and optionally making use of concurrency through coroutines and / or threads.

Asynchronous programming is a strategy for deferring operations with high latency or low priority, usually in an attempt to improve performance, responsiveness, and / or composability of software. Such strategies are usually employed using some combination of event-driven programming and callbacks, and optionally making use of concurrency through coroutines and / or threads.

Asynchronous programming is used in many situations:

  • handling user input in UIs and games,
  • processing network traffic,
  • performing disk I/O,
  • batching work,
  • and more.

Asynchronous programming models can aid in software composition in many languages (notably languages where functions are first-class types) and APIs providing callback-based completion messaging. Proper use of this programming methodology can improve throughput and improve responsiveness by reducing total latency of job batches when executed in parallel. This approach can also result in an increase in system throughput at the cost of increased operational latency resulting from deferred processing.

50995 questions
323
votes
15 answers

Call An Asynchronous Javascript Function Synchronously

First, this is a very specific case of doing it the wrong way on-purpose to retrofit an asynchronous call into a very synchronous codebase that is many thousands of lines long and time doesn't currently afford the ability to make the changes to "do…
Robert C. Barth
  • 22,687
  • 6
  • 45
  • 52
297
votes
17 answers

Callback after all asynchronous forEach callbacks are completed

As the title suggests. How do I do this? I want to call whenAllDone() after the forEach-loop has gone through each element and done some asynchronous processing. [1, 2, 3].forEach( function(item, index, array, done) { asyncFunction(item,…
Dan Andreasson
  • 15,380
  • 5
  • 29
  • 30
291
votes
6 answers

asyncio.gather vs asyncio.wait (vs asyncio.TaskGroup)

asyncio.gather and asyncio.wait seem to have similar uses: I have a bunch of async things that I want to execute/wait for (not necessarily waiting for one to finish before the next one starts). Since Python 3.11 there is yet another similar feature,…
Claude
  • 8,806
  • 4
  • 41
  • 56
283
votes
4 answers

Why would one use Task over ValueTask in C#?

As of C# 7.0 async methods can return ValueTask. The explanation says that it should be used when we have a cached result or simulating async via synchronous code. However I still do not understand what is the problem with using ValueTask always…
Stilgar
  • 22,354
  • 14
  • 64
  • 101
281
votes
13 answers

Calling async method synchronously

I have an async method: public async Task GenerateCodeAsync() { string code = await GenerateCodeService.GenerateCodeAsync(); return code; } I need to call this method from a synchronous method. How can I do this without having to…
Catalin
  • 11,503
  • 19
  • 74
  • 147
272
votes
8 answers

What is the difference between launch/join and async/await in Kotlin coroutines

In the kotlinx.coroutines library you can start new coroutine using either launch (with join) or async (with await). What is the difference between them?
Roman Elizarov
  • 27,053
  • 12
  • 64
  • 60
269
votes
13 answers

How to read file with async/await properly?

I cannot figure out how async/await works. I slightly understand it but I can't make it work. function loadMonoCounter() { fs.readFileSync("monolitic.txt", "binary", async function(err, data) { return await new Buffer( data); …
Jeremy Dicaire
  • 4,615
  • 8
  • 38
  • 62
269
votes
5 answers

Difference between CompletableFuture, Future and RxJava's Observable

I would like to know the difference between CompletableFuture,Future and Observable RxJava. What I know is all are asynchronous but Future.get() blocks the thread CompletableFuture gives the callback methods RxJava Observable --- similar to…
shiv455
  • 7,384
  • 19
  • 54
  • 93
267
votes
18 answers

How to use JUnit to test asynchronous processes

How do you test methods that fire asynchronous processes with JUnit? I don't know how to make my test wait for the process to end (it is not exactly a unit test, it is more like an integration test as it involves several classes and not just one).
Sam
  • 6,437
  • 6
  • 33
  • 41
256
votes
4 answers

Using Moq to mock an asynchronous method for a unit test

I am testing a method for a service that makes a Web API call. Using a normal HttpClient works fine for unit tests if I also run the web service (located in another project in the solution) locally. However when I check in my changes the build…
mvanella
  • 3,456
  • 3
  • 19
  • 23
252
votes
25 answers

React - Display loading screen while DOM is rendering?

This is an example from the Google AdSense application page. The loading screen is displayed before the main page is shown. I am not sure how to achieve the same effect with React because if I render the loading screen as a React component, it will…
Thanh Nguyen
  • 5,174
  • 11
  • 43
  • 74
250
votes
7 answers

How to use the 'main' parameter in package.json?

I have done quite some search already. However, still having doubts about the 'main' parameter in the package.json of a Node project. How would filling in this field help? Asking in another way, can I start the module in a different style if this…
Gavin
  • 4,458
  • 3
  • 24
  • 37
247
votes
18 answers

How to make HTTP requests in PHP and not wait on the response

Is there a way in PHP to make HTTP calls and not wait for a response? I don't care about the response, I just want to do something like file_get_contents(), but not wait for the request to finish before executing the rest of my code. This would be…
Brent
  • 23,354
  • 10
  • 44
  • 49
245
votes
3 answers

Understanding dispatch_async

I have question around this code dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL]; [self…
user2332873
  • 2,569
  • 3
  • 13
  • 7
244
votes
8 answers

When should I use Async Controllers in ASP.NET MVC?

I have some concerns using async actions in ASP.NET MVC. When does it improve performance of my apps, and when does it not? Is it good to use async action everywhere in ASP.NET MVC? Regarding awaitable methods: shall I use async/await keywords when…
Vnuuk
  • 6,177
  • 12
  • 40
  • 53