async-await

Async Await Recursion in .NET 4.8 causes StackoverflowException (not in .Net Core 3.1!)

半世苍凉 提交于 2020-04-10 14:48:27
问题 Why does the following code cause a StackOverflowException in .Net4.8 with only a 17-depth recursion? However this does not happen in NetCore 3.1 (I can set the count to 10_000 and it still works) class Program { static async Task Main(string[] args) { try { await TestAsync(17); } catch(Exception e) { Console.WriteLine("Exception caught: " + e); } } static async Task TestAsync(int count) { await Task.Run(() => { if (count <= 0) throw new Exception("ex"); }); Console.WriteLine(count); await

Is it correct to use await setState()?

喜你入骨 提交于 2020-04-10 09:25:14
问题 my function looks like this: this.setState(prevState => ({ time : prevState.time + 1 }), function() { doSomethingWithNewState(this.state.time) }) is it correct to use await in this situation? like this: await this.setState(prevState => ({ time : prevState.time + 1 })); doSomethingWithNewState(this.state.time); 回答1: No this.setState doesn't return a promise. So you can't use await in this case. You need to use the callback. 回答2: As the previous answer mentioned, setState() does not return a

async function with the class in javascript

烈酒焚心 提交于 2020-04-10 09:20:47
问题 I have create a class in nodejs class ApnService { sendNotification(deviceType, deviceToken, msg, type, id) { try { const note = await apnProvider.send(note, deviceToken) console.log(note) } catch (err) { console.log(err) } } } export default ApnService What I need to do is to convert above function to async . But when I use below syntax It throws me error SyntaxError: src/services/apn.js: Unexpected token (43:19) 41 | } 42 | > 43 | sendNotification = async(deviceType, deviceToken, msg, type,

Exception from Func<> not caught (async)

时间秒杀一切 提交于 2020-04-10 09:09:00
问题 I'm having the following piece of code (simplified in order to make this repro). Obviously, the catch exception block will contain more logic. I am having the following piece of code: void Main() { var result = ExecuteAction(async() => { // Will contain real async code in production throw new ApplicationException("Triggered exception"); } ); } public virtual TResult ExecuteAction<TResult>(Func<TResult> func, object state = null) { try { return func(); } catch (Exception ex) { // This part is

Exception from Func<> not caught (async)

冷暖自知 提交于 2020-04-10 09:08:05
问题 I'm having the following piece of code (simplified in order to make this repro). Obviously, the catch exception block will contain more logic. I am having the following piece of code: void Main() { var result = ExecuteAction(async() => { // Will contain real async code in production throw new ApplicationException("Triggered exception"); } ); } public virtual TResult ExecuteAction<TResult>(Func<TResult> func, object state = null) { try { return func(); } catch (Exception ex) { // This part is

Flutter read shared preferences in main then decide which startup page?

回眸只為那壹抹淺笑 提交于 2020-04-10 07:38:07
问题 I want to judge which page to start up in main (actually is login page and the home page). So I have to read isLogin in preferences. How to do that in main? I tied these codes: Future<Null> checkIsLogin() async { String _token = ""; // If token exist already, then HomePage SharedPreferences prefs = await SharedPreferences.getInstance(); _token = prefs.getString("token"); print('get token from prefs: ' + _token); if (_token != "" && _token != null) { // already login print("alreay login.");

Using Linq to Sql asynchronously with new async/await

柔情痞子 提交于 2020-04-09 21:06:54
问题 What are best practices to use L2S with new C# 5 async/await keywords comparing to this approach? Couldn't find any on web. 回答1: EF 5 does not have async/await support, but the open source version is actively looking into possibilities here. EDIT: the Async support in EF is documented at http://msdn.microsoft.com/en-us/data/jj819165.aspx. It doesn't stream the results in as they are hydrated (as you would find with RX) but it does make the database calls asynchronous. As for LINQ to SQL,

Using Linq to Sql asynchronously with new async/await

这一生的挚爱 提交于 2020-04-09 21:01:33
问题 What are best practices to use L2S with new C# 5 async/await keywords comparing to this approach? Couldn't find any on web. 回答1: EF 5 does not have async/await support, but the open source version is actively looking into possibilities here. EDIT: the Async support in EF is documented at http://msdn.microsoft.com/en-us/data/jj819165.aspx. It doesn't stream the results in as they are hydrated (as you would find with RX) but it does make the database calls asynchronous. As for LINQ to SQL,

chrome.runtime.onMessage response with async await

心不动则不痛 提交于 2020-04-07 18:44:28
问题 I want to use async await in an onMessage listener: chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) =>{ var key = await getKey(); sendResponse(key); }); However I get undefined when I send a message. From the documentation for chrome.runtime.onMessage.addListener: This function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open

Get response from axios with await/async

試著忘記壹切 提交于 2020-04-07 16:12:29
问题 I'm trying to get JSON object from axios 'use strict' async function getData() { try { var ip = location.host; await axios({ url: http() + ip + '/getData', method: 'POST', timeout: 8000, headers: { 'Content-Type': 'application/json', } }).then(function (res) { console.dir(res); // we are good here, the res has the JSON data return res; }).catch(function (err) { console.error(err); }) } catch (err) { console.error(err); } } Now I need to fetch the res let dataObj; getData().then(function