Promise.all is returning an array of undefined and resolves before being done

Promise.all accepts an Array of Promise objects. You’re getting an Array of undefined because you’re not returning any Promise in your map callback: function addText(queries) { return Promise.all(queries.map(function(query) { // Add `return` here or the `map` returns an Array of `undefined`. return models.queries .findById(query.queryId, { raw: true, attributes: [ “query” ] }) .then(function(queryFetched) { query.text … Read more

Does an asynchronous call always create/call a new thread?

This is an interesting question. Asynchronous programming is a paradigm of programming that is principally single threaded, i.e. “following one thread of continuous execution”. You refer to javascript, so lets discuss that language, in the environment of a web browser. A web browser runs a single thread of javascript execution in each window, it handles … Read more

Spring @Async limit number of threads

If you are using Spring’s Java-configuration, your config class needs to implements AsyncConfigurer: @Configuration @EnableAsync public class AppConfig implements AsyncConfigurer { […] @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); executor.setMaxPoolSize(5); executor.setQueueCapacity(50); executor.setThreadNamePrefix(“MyExecutor-“); executor.initialize(); return executor; } } See @EnableAsync documentation for more details : http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html

Calling TaskCompletionSource.SetResult in a non blocking manner

I’ve discovered that TaskCompletionSource.SetResult(); invokes the code awaiting the task before returning. In my case that result in a deadlock. Yes, I have a blog post documenting this (AFAIK it’s not documented on MSDN). The deadlock happens because of two things: There’s a mixture of async and blocking code (i.e., an async method is calling … Read more

What is the difference between Asynchronous calls and Callbacks

Very simply, a callback needn’t be asynchronous. http://docs.apigee.com/api-baas/asynchronous-vs-synchronous-calls Synchronous: If an API call is synchronous, it means that code execution will block (or wait) for the API call to return before continuing. This means that until a response is returned by the API, your application will not execute any further, which could be perceived by … Read more

ReaderWriterLockSlim and async\await

ReaderWriterLockSlim is a thread-affine lock type, so it usually cannot be used with async and await. You should either use SemaphoreSlim with WaitAsync, or (if you really need a reader/writer lock), use my AsyncReaderWriterLock from AsyncEx or Stephen Toub’s AsyncReaderWriterLock.

Asynchronous constructor

Update 2: Here is an updated example using an asynchronous factory method. N.B. this requires Node 8 or Babel if run in a browser. class Element { constructor(nucleus){ this.nucleus = nucleus; } static async createElement(){ const nucleus = await this.loadNucleus(); return new Element(nucleus); } static async loadNucleus(){ // do something async here and return it … Read more

Node.js – Using the async lib – async.foreach with object

The final function does not get called because async.forEach requires that you call the callback function for every element. Use something like this: async.forEach(Object.keys(dataObj), function (item, callback){ console.log(item); // print the key // tell async that that particular element of the iterator is done callback(); }, function(err) { console.log(‘iterating done’); });

What is the best practice in EF Core for using parallel async calls with an Injected DbContext?

Using any context.XyzAsync() method is only useful if you either await the called method or return control to a calling thread that’s doesn’t have context in its scope. A DbContext instance isn’t thread-safe: you should never ever use it in parallel threads. Which means, just for sure, never use it in multiple threads anyway, even … Read more