async-await

Creating an async singletone in javascript

Deadly 提交于 2020-01-16 08:36:03
问题 I need to implement an async singleton, that creates a single instance of a class that requires asynchronous operations in order to pass arguments to the constructor. I have the following code: class AsyncComp { constructor(x, y) { this.x = x; this.y = y; } // A factory method for creating the async instance static async createAsyncInstance() { const info = await someAsyncFunction(); return new AsyncComp(info.x, info.y); } // The singleton method static getAsyncCompInstance() { if

Should I use async await on UI Thread or Task.Run to process multiple files?

老子叫甜甜 提交于 2020-01-16 01:09:32
问题 I am writing an application in WPF using .NET 4.5. The application allows the user to select multiple files and import them. When this happens the application will parse text files and store the data into a database. The files can be very large so I want the UI to remain responsive and allow the user to do other things while this is happening. I'm new to asynchronous programming and would to know what the best approach would be? According to a Microsoft article... "The async and await

await task.delay helps in UI refresh faster, but how?

大憨熊 提交于 2020-01-15 16:44:20
问题 I have a view model that is fetching a row of records and displaying on the Windows phone UI.. This view model method which fetches the data is doing a lot of Tasks, all marked with Await operations.. Looks like below: async Task GetData() { var dataCollection = await GetSomeData(); await DoThis(); await DoThat(); } The UI refreshes after the 'DoThis' call is invoked. Now I just observed that if I introduce a Task.Delay in the code before other Tasks are done, the UI is refreshed immediately.

Call async method without await

China☆狼群 提交于 2020-01-15 16:44:12
问题 I working on image loader library for Windows Phone 8+ applications, and of course it supports caching on disk. So, I need to save image on disk asynchronously without awaiting result: // Async saving to the storage cache without await // ReSharper disable once CSharpWarnings::CS4014 Config.StorageCacheImpl.SaveAsync(imageUrl, downloadResult.ResultStream) .ContinueWith( task => { if (task.IsFaulted || !task.Result) { Log("[error] failed to save in storage: " + imageUri); } } ); As you can see

Calling a 3rd party async Task method synchronously

不想你离开。 提交于 2020-01-15 12:15:40
问题 I'm reading hundreds of answers about await / async , but there is still no answer to this simple question. There is an awaitable method, written by someone else and it is necessary to wait till it finishes. // cannot be changed public async Task ThirdPartyAsync(...) { // calls other awaitable methods // takes few seconds } // I'd like something like this public void MyPatientCallingMethod(...) { // 1. call the method ThirdPartyAsync(...); // 2. wait for ThirdPartyAsync to finish (I don't

Top-level await doesn't work in the latest Node.js

流过昼夜 提交于 2020-01-15 10:09:48
问题 Is top-level await still not supported in Node.js (Jan 2020, Node.js 13.5.0)? I've tried some tutorials, like this one, but still no luck, always getting the same error: D:\NodeJS>node --experimental-modules test.js (node:17724) ExperimentalWarning: The ESM module loader is experimental. file:///D:/NodeJS/test.js:9 await test(); ^^^^^ SyntaxError: Unexpected reserved word The entire file content: function test() { } await test(); I have tried using "type": "module" in package.json , and

Ensuring message sequence in Orleans in a pipelined execution

我的梦境 提交于 2020-01-15 09:40:49
问题 I have three grains (A, B and C) doing different jobs in a pipeline. GrainA will pass the result to grainB, and grainB will pass the result to grainC. I want to guarantee sequential message sending between successive grains which can be achieved by below. // client code foreach(var i in list) { await grainA.job(i); } //grain A code async Task job(var i) { DoSomeWorkA(i); await grainB.job(i); } //grain B code async Task job(var i) { DoSomeWorkB(i); await grainC.job(i); } //grain C code async

How to make Dispose await for all async methods?

不问归期 提交于 2020-01-15 09:00:29
问题 I have disposable class with async methods. class Gateway : IDisposable { public Gateway() {} public void Dispose() {} public async Task<Data> Request1 () {...} public async Task<Data> Request2 () {...} public async Task<Data> Request3 () {...} } I need Dispose to await until all running requests are completed. So, either I need to track of all running tasks, or use AsyncLock from AsyncEx, or something else? Updated As I can see someone is afraid of blocking Dispose. Then we could make Task

Continuation tasks not executing in correct order

邮差的信 提交于 2020-01-15 08:37:05
问题 Been trying to execute tasks sequentially but they are executed in a random order instead. Appending .Unwrap after .ContinueWith doesn't help Returning a Task of T from these methods instead of Task and assigning their result in the caller doesn't work either Not sure about signature of my methods, whether they should contain async/await or not. Sequencing tasks : Task biographies = LoadArtistBiographies(apiKey); Task blogs = LoadArtistBlogs(apiKey); Task familiarity = LoadArtistFamiliarity

How to catch async void method exception?

旧街凉风 提交于 2020-01-15 05:27:06
问题 I have an implementation like this: Task<IEnumerable<Item1>> GetItems1() { return RunRequest(async () => ParseItemsFromResponse(await(httpClient.Get(..)))); } Task<IEnumerable<Item2>> GetItems2() { return RunRequest(async () => ParseItemsFromResponse(await httpClient.Get(..))); } TResult RunRequest<TResult>(Func<TResult> req) { try { return req(); } catch (Exception ex) { // Parse exception here and throw custom exceptions } } The issue is the void anonymous method async () =>