async-await

Use Task.WaitAll() to handle awaited tasks?

痞子三分冷 提交于 2019-12-28 05:49:11
问题 Ideally what I want to do is to delay a task with a non-blocking mode and then wait for all the tasks to complete. I've tried to add the task object returned by Task.Delay and then use Task.WaitAll but seems this won't help. How should I solve this problem? class Program { public static async void Foo(int num) { Console.WriteLine("Thread {0} - Start {1}", Thread.CurrentThread.ManagedThreadId, num); var newTask = Task.Delay(1000); TaskList.Add(newTask); await newTask; Console.WriteLine("Thread

can not await async lambda

懵懂的女人 提交于 2019-12-28 03:32:04
问题 Consider this, Task task = new Task (async () =>{ await TaskEx.Delay(1000); }); task.Start(); task.Wait(); The call task.Wait() does not wait for the task completion and the next line is executed immediately, but if I wrap the async lambda expression into a method call, the code works as expected. private static async Task AwaitableMethod() { await TaskEx.Delay(1000); } then (updated according comment from svick) await AwaitableMethod(); 回答1: In your lambda example, when you call task.Wait()

Transpile Async Await proposal with Babel.js?

对着背影说爱祢 提交于 2019-12-28 02:26:06
问题 There is a proposal for introducing C# style async-await . I know Babel.js transpiles ES6 to ES5, but is there any way to make it transpile async-await to ES5 ? 回答1: Babel v6 As of Babel v6, Babel doesn't contain any transformers itself anymore. You have to explicitly specify any feature you want to transform. Presets - non ES2015 environment The quickest way to get this working is to use presets which already contain the set of plugins needed to transform ES2015 and newer proposals. For

try..catch not catching async/await errors

大城市里の小女人 提交于 2019-12-28 01:28:31
问题 Perhaps I misunderstood how catching errors with async/await is supposed to work from things articles like this https://jakearchibald.com/2014/es7-async-functions/ and this http://pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.html, but my catch block is not catching 400/500. async () => { let response try { let response = await fetch('not-a-real-url') } catch (err) { // not jumping in here. console.log(err) } }() example on codepen if it helps 回答1: 400/500 is not an error, it's a

Use Task.Run() in synchronous method to avoid deadlock waiting on async method?

ぃ、小莉子 提交于 2019-12-27 19:10:10
问题 UPDATE The purpose of this question is to get a simple answer about Task.Run() and deadlocking. I very much understand the theoretical reasoning for not mixing async and sync, and I take them to heart. I'm not above learning new things from others; I seek to do that whenever I can. There's just times when all a guy needs is a technical answer... I have a Dispose() method that needs to call an async method. Since 95% of my code is async, refactoring isn't the best choice. Having an

What is the difference between JavaScript promises and async await?

廉价感情. 提交于 2019-12-27 10:32:31
问题 I have been using ECMAScript 6 and ECMAScript 7 features already (thanks to Babel) in my applications - both mobile and web. The first step obviously was to ECMAScript 6 levels. I learnt many async patterns, the promises (which are really promising), generators (not sure why the * symbol), etc. Out of these, promises suited my purpose pretty well. And I have been using them in my applications quite a lot. Here is an example/pseudocode of how I have implemented a basic promise- var myPromise =

What is the difference between JavaScript promises and async await?

本秂侑毒 提交于 2019-12-27 10:32:19
问题 I have been using ECMAScript 6 and ECMAScript 7 features already (thanks to Babel) in my applications - both mobile and web. The first step obviously was to ECMAScript 6 levels. I learnt many async patterns, the promises (which are really promising), generators (not sure why the * symbol), etc. Out of these, promises suited my purpose pretty well. And I have been using them in my applications quite a lot. Here is an example/pseudocode of how I have implemented a basic promise- var myPromise =

Regarding usage of Task.Start() , Task.Run() and Task.Factory.StartNew()

淺唱寂寞╮ 提交于 2019-12-27 10:21:53
问题 I just saw 3 routines regarding TPL usage which do the same job; here is the code: public static void Main() { Thread.CurrentThread.Name = "Main"; // Create a task and supply a user delegate by using a lambda expression. Task taskA = new Task( () => Console.WriteLine("Hello from taskA.")); // Start the task. taskA.Start(); // Output a message from the calling thread. Console.WriteLine("Hello from thread '{0}'.", Thread.CurrentThread.Name); taskA.Wait(); } public static void Main() { Thread

Regarding usage of Task.Start() , Task.Run() and Task.Factory.StartNew()

末鹿安然 提交于 2019-12-27 10:20:54
问题 I just saw 3 routines regarding TPL usage which do the same job; here is the code: public static void Main() { Thread.CurrentThread.Name = "Main"; // Create a task and supply a user delegate by using a lambda expression. Task taskA = new Task( () => Console.WriteLine("Hello from taskA.")); // Start the task. taskA.Start(); // Output a message from the calling thread. Console.WriteLine("Hello from thread '{0}'.", Thread.CurrentThread.Name); taskA.Wait(); } public static void Main() { Thread

async/await implicitly returns promise?

帅比萌擦擦* 提交于 2019-12-27 10:20:12
问题 I read that async functions marked by the async keyword implicitly return a promise: async function getVal(){ return await doSomethingAync(); } var ret = getVal(); console.log(ret); but that is not coherent...assuming doSomethingAsync() returns a promise, and the await keyword will return the value from the promise, not the promise itsef, then my getVal function should return that value, not an implicit promise. So what exactly is the case? Do functions marked by the async keyword implicitly