async-await

Use case for FutureProvider

☆樱花仙子☆ 提交于 2020-06-13 00:30:51
问题 I was wondering what use case exists for the FutureProvider class? I am particularly interested how to load an asynchronous function like Future<Contact> getAllContacts() async { ... } when the ui is built with a FutureProvider , ChangeNotifierProvider or anything similar. Moreover, each time when notifyListeners() is called, I would like to have that provider to rebuild the ui with asynchronous call. Is that any way possible with provider or am I missing something? 回答1: FutureProvider doesn

Async Await Progress Reporting Not Working

自作多情 提交于 2020-06-12 06:39:12
问题 I have a C# WPF program that opens a file, reads it line by line, manipulates each line then writes the line out to another file. That part worked fine. I wanted to add some progress reporting so I made the methods async and used await with progress reporting. The progress reporting is super simple - just update a label on the screen. Here is my code: async void Button_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title =

async await fetch undefined. How to handle?

拈花ヽ惹草 提交于 2020-06-11 22:47:39
问题 I am currently learning async await fetch and I've created the following example to help me learn. The working example below: fetches three random json records from a Public API extracts the url from each return json creates three img elements appends three img elements to the document body. Notice that promise2 has an intentionally wrong path set to force a http status 404. How do I handle this error if it was to happen to any of the three promises? // the big promise. async function

Do Entity Framework async methods consume ThreadPool threads?

我与影子孤独终老i 提交于 2020-06-10 03:41:46
问题 I usually use many EF Core async methods in my web application like this: await db.Parents.FirstOrDefaultAsync(p => p.Id == id); As we know, initial number of threads in ThreadPool by default is limited to number of CPU logical cores. Also user requests are handled by threads in ThreadPool . Should I worry about handling user requests or performance issues due to many async calls in my application? 回答1: Should I worry about handling user requests or performance issues due to many async calls

Understanding the use of Task.Run + Wait() + async + await used in one line

左心房为你撑大大i 提交于 2020-06-10 02:54:26
问题 I'm a C# newbie, so I'm struggling to understand some concepts, and I run into a piece of code that I'm not quite understanding: static void Main(string[] args) { Task.Run(async () => { await SomeClass.Initiate(new Configuration()); }).Wait(); while (true) ; } As I understand, this runs a task which initiates a method. This method runs, and then, once it finished, it gets into an infinite loop waiting. It feels that either the code doesn't make sense, or that I'm not understanding right.

What is correct way to combine long-running tasks with async / await pattern?

假如想象 提交于 2020-06-10 02:20:48
问题 I have a "High-Precision" timer class that I need to be able to be start, stop & pause / resume. To do this, I'm tying together a couple of different examples I found on the internet, but I'm not sure if I'm using Tasks with asnyc / await correctly. Here is my relevant code: //based on http://haukcode.wordpress.com/2013/01/29/high-precision-timer-in-netc/ public class HighPrecisionTimer : IDisposable { Task _task; CancellationTokenSource _cancelSource; //based on http://blogs.msdn.com/b

Why can't I 'yield from' inside an async function?

不羁的心 提交于 2020-06-10 02:19:49
问题 In Python 3.6, I am able to use yield inside a coroutine. However I am not able to use yield from . Below is my code. On line 3 I await another coroutine. On line 4 I try to yield from a file. Why won't Python 3.6 allow me to do that? async def read_file(self, filename): with tempfile.NamedTemporaryFile(mode='r', delete=True, dir='/tmp', prefix='sftp') as tmp_file: await self.copy_file(filename, tmp_file) yield from open(tmp_file) Here's the exception Python 3.6 raises for the above code:

Why can't I 'yield from' inside an async function?

十年热恋 提交于 2020-06-10 02:19:36
问题 In Python 3.6, I am able to use yield inside a coroutine. However I am not able to use yield from . Below is my code. On line 3 I await another coroutine. On line 4 I try to yield from a file. Why won't Python 3.6 allow me to do that? async def read_file(self, filename): with tempfile.NamedTemporaryFile(mode='r', delete=True, dir='/tmp', prefix='sftp') as tmp_file: await self.copy_file(filename, tmp_file) yield from open(tmp_file) Here's the exception Python 3.6 raises for the above code:

Is having a return type of Task enough to make a method run asynchronously?

大城市里の小女人 提交于 2020-06-09 07:10:20
问题 I have a simple method that does a complicated string operation and returns the result. As you can see, the return type of this method is Task<string> . Therefore, I can use Task.FromResult(result) to return the value of my string. public Task<string> ComplexOperation() { string result = // Do something complex return Task.FromResult(result); } Then, I can use the await keyword to call this method. public static async Task Main(string[] args) { var myResult = await ComplexOperation(); }

How to use Array.prototype.filter with async?

落花浮王杯 提交于 2020-06-07 18:53:52
问题 Background I am trying to filter an array of objects. Before I filter, I need to convert them to some format, and this operation is asynchronous. const convert = () => new Promise( resolve => { setTimeout( resolve, 1000 ); }); So, my first try was to do something like the following using async/await: const objs = [ { id: 1, data: "hello" }, { id: 2, data: "world"} ]; objs.filter( async ( obj ) => { await convert(); return obj.data === "hello"; }); Now, as some of you may know, Array.protoype