async-await

Wait for async Task without wrapping exceptions in AggregateException

ⅰ亾dé卋堺 提交于 2020-06-07 09:22:30
问题 I am using a library which provides methods ending with ...Async and return Task . I am going to use these in a command line application . So I need to call them synchronously a lot. C# of course does not allow calling these methods in Main method since you cannot use async modifier on Main method. Assume this is the task: var task = datastore.Save(data); I found several solutions like: Tasks.WaitAll(task); task.Wait(); however all these wrap thrown exceptions in AggregateException , I don't

How can I store an async function in a struct and call it from a struct instance?

£可爱£侵袭症+ 提交于 2020-06-07 04:41:09
问题 I'm trying to achieve this with the new async / await syntax, std::future::Future s and a recent version of Tokio. I'm using Tokio 0.2.0-alpha.4 and Rust 1.39.0-nightly . Different things I've tried include: using Box<dyn> s for the types that I want to store in the struct using generics in the struct definition I couldn't quite get a minimal working version, so here's a simplified version of what I'm trying to achieve: async fn foo(x: u8) -> u8 { 2 * x } // type StorableAsyncFn = Fn(u8) ->

for await of VS Promise.all

一笑奈何 提交于 2020-06-06 07:52:12
问题 Is there any difference between this: const promises = await Promise.all(items.map(e => somethingAsync(e))); for (const res of promises) { // do some calculations } And this ? for await (const res of items.map(e => somethingAsync(e))) { // do some calculations } I know that in the first snippet, all the promises are fired at the same time but I'm not sure about the second. Does the for loop wait for the first iteration to be done to call the next promise ? Or are all the promises fired at the

async await promise.all map not resolving promises

余生颓废 提交于 2020-06-01 06:22:34
问题 i'm trying to understand Promise.all in a map function using async await but i seem to returning promises which are pending, i don't understand why Here is the working code when i have a .then to resolve my promise const arr = [1, 2, 3, 4, 5]; const results = Promise.all(arr.map( item => { return item + 1; })); results.then(result=> console.log(result)) which logs as expected = [ 2, 3, 4, 5, 6 ] now to implement the function using async-await i know i need to wrap the function in an async

How to save results of a promise to a variable? [duplicate]

試著忘記壹切 提交于 2020-05-31 07:10:19
问题 This question already has answers here : How do I return the response from an asynchronous call? (39 answers) Closed last year . I need to fetch some JSON data from an API and assign the result to a variable. I can see the array of data in the console, but [abc] is always set as a Pending promise. I've tried to assign [abc] instead of logging [data] or simply returning [data] but it's not working. Ideally, I would also want to stop the execution of the following code until I get the required

Blazor WebAssembly: How to get UI to update during long running, non-async process

半腔热情 提交于 2020-05-30 11:25:49
问题 I have some complex calculations that take a while to do. (Yes, I know, client-side may not sound like the ideal place to do them, but there are good reasons for it.) I would like the page to update with results as the calculations progress. I can get the UI to re-render as the calculations progress, but not update the results correctly. Note that the calculations are not inherently async and wrapping them in Task.Run does not seem to help. Here is a simplified version of the code that

Blazor WebAssembly: How to get UI to update during long running, non-async process

别来无恙 提交于 2020-05-30 11:24:43
问题 I have some complex calculations that take a while to do. (Yes, I know, client-side may not sound like the ideal place to do them, but there are good reasons for it.) I would like the page to update with results as the calculations progress. I can get the UI to re-render as the calculations progress, but not update the results correctly. Note that the calculations are not inherently async and wrapping them in Task.Run does not seem to help. Here is a simplified version of the code that

Blazor WebAssembly: How to get UI to update during long running, non-async process

╄→гoц情女王★ 提交于 2020-05-30 11:21:00
问题 I have some complex calculations that take a while to do. (Yes, I know, client-side may not sound like the ideal place to do them, but there are good reasons for it.) I would like the page to update with results as the calculations progress. I can get the UI to re-render as the calculations progress, but not update the results correctly. Note that the calculations are not inherently async and wrapping them in Task.Run does not seem to help. Here is a simplified version of the code that

Blazor WebAssembly: How to get UI to update during long running, non-async process

£可爱£侵袭症+ 提交于 2020-05-30 11:20:32
问题 I have some complex calculations that take a while to do. (Yes, I know, client-side may not sound like the ideal place to do them, but there are good reasons for it.) I would like the page to update with results as the calculations progress. I can get the UI to re-render as the calculations progress, but not update the results correctly. Note that the calculations are not inherently async and wrapping them in Task.Run does not seem to help. Here is a simplified version of the code that

Avoiding use of ActionBlock<TInput>.Post when PostDataflowBlockOptions.BoundedCapacity is not the default value?

十年热恋 提交于 2020-05-30 08:14:09
问题 The bounty expires in 6 days . Answers to this question are eligible for a +100 reputation bounty. SpiritBob wants to reward an existing answer . I've heard that you can lose information if you use the Post method instead of the SendAsync method of an ActionBlock<T> object, when you decide to utilize it's BoundedCapacity property. Could someone please explain why that is so? 回答1: The Post method attempts to post an item synchronously and returns true or false , depending on whether the block