Skip to main content

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.

Filter by
Sorted by
Tagged with
6708 votes
42 answers
2.1m views

How do I return the response from an asynchronous call?

How do I return the response/result from a function foo that makes an asynchronous request? I am trying to return the value from the callback, as well as assigning the result to a local variable ...
Felix Kling's user avatar
3148 votes
34 answers
1.5m views

How can I upload files asynchronously with jQuery?

I would like to upload a file asynchronously with jQuery. $(document).ready(function () { $("#uploadbutton").click(function () { var filename = $("#file").val(); $.ajax({...
Sergio del Amo's user avatar
1414 votes
26 answers
1.2m views

How and when to use ‘async’ and ‘await’

From my understanding one of the main things that async and await do is to make code easy to write and read - but is using them equal to spawning background threads to perform long duration logic? I'...
Dan Dinu's user avatar
  • 33.2k
1391 votes
16 answers
1.3m views

How to call asynchronous method from synchronous method in C#?

I have a public async Task Foo() method that I want to call from a synchronous method. So far all I have seen from MSDN documentation is calling async methods via async methods, but my whole program ...
Tower's user avatar
  • 101k
1361 votes
22 answers
1.1m views

Asynchronous vs synchronous execution. What is the difference? [closed]

What is the difference between asynchronous and synchronous execution?
tush1r's user avatar
  • 19.5k
1328 votes
14 answers
834k views

How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

I have a JavaScript widget which provides standard extension points. One of them is the beforecreate function. It should return false to prevent an item from being created. I've added an Ajax call ...
Artem Tikhomirov's user avatar
916 votes
7 answers
305k views

Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

Given the following examples, why is outerScopeVar undefined in all cases? var outerScopeVar; var img = document.createElement('img'); img.onload = function() { outerScopeVar = this.width; }; img....
Fabrício Matté's user avatar
904 votes
13 answers
223k views

Why do we need middleware for async flow in Redux?

According to the docs, "Without middleware, Redux store only supports synchronous data flow". I don't understand why this is the case. Why can't the container component call the async API, and then ...
Stas Bichenko's user avatar
894 votes
12 answers
568k views

Call async/await functions in parallel

As far as I understand, in ES7/ES2016 putting multiple await's in code will work similar to chaining .then() with promises, meaning that they will execute one after the other rather than in parallel. ...
Victor Marchuk's user avatar
764 votes
23 answers
556k views

How would I run an async Task<T> method synchronously?

I am learning about async/await, and ran into a situation where I need to call an async method synchronously. How can I do that? Async method: public async Task<Customers> GetCustomers() { ...
Rachel's user avatar
  • 132k
732 votes
6 answers
607k views

async/await - when to return a Task vs void?

Under what scenarios would one want to use public async Task AsyncMethod(int num) instead of public async void AsyncMethod(int num) The only scenario that I can think of is if you need the task ...
user981225's user avatar
  • 9,132
622 votes
12 answers
179k views

What are the best use cases for Akka framework [closed]

I have heard lots of raving about Akka framework (Java/Scala service platform), but so far have not seen many actual examples of use cases it would be good for. So I would be interested in hearing ...
StaxMan's user avatar
  • 115k
579 votes
19 answers
328k views

Can't specify the 'async' modifier on the 'Main' method of a console app

I am new to asynchronous programming with the async modifier. I am trying to figure out how to make sure that my Main method of a console application actually runs asynchronously. class Program { ...
danielovich's user avatar
  • 9,497
579 votes
3 answers
503k views

How to return value from an asynchronous callback function? [duplicate]

This question is asked many times in SO. But still I can't get stuff. I want to get some value from callback. Look at the script below for clarification. function foo(address){ // google map ...
Gowri's user avatar
  • 16.8k
523 votes
4 answers
186k views

WaitAll vs WhenAll

What is the difference between Task.WaitAll() and Task.WhenAll() from the Async CTP? Can you provide some sample code to illustrate the different use cases?
Yaron Levi's user avatar
  • 12.9k
489 votes
16 answers
174k views

asynchronous and non-blocking calls? also between blocking and synchronous

What is the difference between asynchronous and non-blocking calls? Also between blocking and synchronous calls (with examples please)?
user331561's user avatar
  • 4,981
487 votes
10 answers
222k views

AngularJS : Initialize service with asynchronous data

I have an AngularJS service that I want to initialize with some asynchronous data. Something like this: myModule.service('MyService', function($http) { var myData = null; $http.get('data....
testing123's user avatar
  • 11.4k
485 votes
8 answers
285k views

How to reject in async/await syntax?

How can I reject a promise that returned by an async/await function? e.g. Originally: foo(id: string): Promise<A> { return new Promise((resolve, reject) => { someAsyncPromise().then((...
Phoenix's user avatar
  • 4,983
456 votes
2 answers
181k views

What is the difference between asynchronous programming and multithreading?

I thought that they were basically the same thing — writing programs that split tasks between processors (on machines that have 2+ processors). Then I'm reading this, which says: Async methods are ...
user5648283's user avatar
  • 6,113
444 votes
12 answers
369k views

JavaScript, Node.js: is Array.forEach asynchronous?

I have a question regarding the native Array.forEach implementation of JavaScript: Does it behave asynchronously? For example, if I call: [many many elements].forEach(function () {lots of work to do})...
R. Gr.'s user avatar
  • 4,481
439 votes
2 answers
443k views

When correctly use Task.Run and when just async-await

I would like to ask you on your opinion about the correct architecture when to use Task.Run. I am experiencing laggy UI in our WPF .NET 4.5 application (with Caliburn Micro framework). Basically I am ...
Lukas K's user avatar
  • 6,239
423 votes
5 answers
308k views

Sleep Command in T-SQL?

Is there to way write a T-SQL command to just make it sleep for a period of time? I am writing a web service asynchronously and I want to be able to run some tests to see if the asynchronous pattern ...
skb's user avatar
  • 30.8k
414 votes
12 answers
216k views

Awaiting multiple Tasks with different results

I have 3 tasks: private async Task<Cat> FeedCat() {} private async Task<House> SellHouse() {} private async Task<Tesla> BuyCar() {} They all need to run before my code can continue ...
Ian Vink's user avatar
  • 68.2k
410 votes
11 answers
460k views

Running multiple async tasks and waiting for them all to complete

I need to run multiple async tasks in a console application, and wait for them all to complete before further processing. There's many articles out there, but I seem to get more confused the more I ...
Daniel Minnaar's user avatar
394 votes
10 answers
76k views

Why use async and return await, when you can return Task<T> directly?

Is there any scenario where writing method like this: public async Task<SomeResult> DoSomethingAsync() { // Some synchronous code might or might not be here... // return await ...
TX_'s user avatar
  • 5,356
382 votes
7 answers
256k views

HttpClient.GetAsync(...) never returns when using await/async

Edit: This question looks like it might be the same problem, but has no responses... Edit: In test case 5 the task appears to be stuck in WaitingForActivation state. I've encountered some odd ...
Benjamin Fox's user avatar
  • 5,732
376 votes
5 answers
242k views

How can I limit Parallel.ForEach?

I have a Parallel.ForEach() async loop with which I download some webpages. My bandwidth is limited so I can download only x pages per time but Parallel.ForEach executes whole list of desired webpages....
eugeneK's user avatar
  • 11k
375 votes
11 answers
108k views

If async-await doesn't create any additional threads, then how does it make applications responsive?

Time and time again, I see it said that using async-await doesn't create any additional threads. That doesn't make sense because the only ways that a computer can appear to be doing more than 1 thing ...
Ms. Corlib's user avatar
  • 5,123
366 votes
15 answers
235k views

How to call an async method from a getter or setter?

What'd be the most elegant way to call an async method from a getter or setter in C#? Here's some pseudo-code to help explain myself. async Task<IEnumerable> MyAsyncMethod() { return await ...
Doguhan Uluca's user avatar
362 votes
6 answers
322k views

Catch an exception thrown by an async void method

Using the async CTP from Microsoft for .NET, is it possible to catch an exception thrown by an async method in the calling method? public async void Foo() { var x = await DoSomethingAsync(); ...
TimothyP's user avatar
  • 21.6k
356 votes
18 answers
154k views

What is the difference between concurrency, parallelism and asynchronous methods?

Concurrency is having two tasks run in parallel on separate threads. However, asynchronous methods run in parallel but on the same 1 thread. How is this achieved? Also, what about parallelism? What ...
GurdeepS's user avatar
  • 66.7k
355 votes
9 answers
328k views

Async await in linq select

I need to modify an existing program and it contains following code: var inputs = events.Select(async ev => await ProcessEventAsync(ev)) .Select(t => t.Result) ...
Alexander Derck's user avatar
340 votes
15 answers
472k views

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 ...
Robert C. Barth's user avatar
317 votes
5 answers
334k views

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 ...
Claude's user avatar
  • 9,675
317 votes
4 answers
100k views

Why would one use Task<T> over ValueTask<T> in C#?

As of C# 7.0 async methods can return ValueTask<T>. 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 ...
Stilgar's user avatar
  • 23.2k
301 votes
11 answers
278k views

Parallel foreach with asynchronous lambda

I would like to handle a collection in parallel, but I'm having trouble implementing it and I'm therefore hoping for some help. The trouble arises if I want to call a method marked async in C#, ...
clausndk's user avatar
  • 3,309
300 votes
13 answers
415k views

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, ...
Dan Andreasson's user avatar
286 votes
13 answers
295k views

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 ...
Jeremy Dicaire's user avatar
286 votes
13 answers
338k views

Calling async method synchronously

I have an async method: public async Task<string> GenerateCodeAsync() { string code = await GenerateCodeService.GenerateCodeAsync(); return code; } I need to call this method ...
Catalin's user avatar
  • 11.7k
281 votes
8 answers
87k views

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's user avatar
278 votes
18 answers
252k views

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 ...
Sam's user avatar
  • 6,588
277 votes
5 answers
132k views

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 ...
shiv455's user avatar
  • 7,684
267 votes
4 answers
187k views

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. ...
mvanella's user avatar
  • 3,616
266 votes
7 answers
211k views

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 ...
Gavin's user avatar
  • 4,638
260 votes
10 answers
312k views

Simplest async/await example possible in Python

I've read many examples, blog posts, questions/answers about asyncio / async / await in Python 3.5+, many were complex, the simplest I found was probably this one. Still it uses ensure_future, and for ...
Basj's user avatar
  • 44.8k
254 votes
25 answers
622k views

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 ...
Thanh Nguyen's user avatar
  • 5,284
249 votes
17 answers
232k views

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 ...
Brent's user avatar
  • 23.6k
248 votes
3 answers
272k views

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]; [...
user2332873's user avatar
  • 2,599
247 votes
4 answers
172k views

Parallel.ForEach vs Task.Run and Task.WhenAll

What are the differences between using Parallel.ForEach or Task.Run() to start a set of tasks asynchronously? Version 1: List<string> strings = new List<string> { "s1", "s2&...
Petter T's user avatar
  • 3,597
246 votes
8 answers
141k views

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 ...
Vnuuk's user avatar
  • 6,467

1
2 3 4 5
1038