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
51,868
questions
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 ...
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({...
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'...
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 ...
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?
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 ...
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....
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 ...
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. ...
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()
{
...
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 ...
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 ...
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
{
...
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 ...
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?
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)?
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....
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((...
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 ...
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})...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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....
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 ...
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 ...
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();
...
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 ...
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)
...
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 ...
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 ...
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 ...
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#, ...
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, ...
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 ...
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 ...
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?
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 ...
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 ...
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.
...
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 ...
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 ...
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 ...
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 ...
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];
[...
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&...
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 ...