async-await

Promise to async await when polling

若如初见. 提交于 2020-01-04 06:36:05
问题 I'm trying to convert a function that uses promise (and polling) to an async function, but I'm not really sure how it works. I have this: function myFunction() { return new Promise(resolve => { // stuff here ... var poll = setInterval(function() { if (condition) { clearInterval(poll); resolve("done"); } }, 100); }); } .. but I'm unsure what to await here: async function myFunction() { // stuff here ... var poll = setInterval(function() { if (condition) { clearInterval(poll); // await what? }

Avoid async from spreading in method signatures

≯℡__Kan透↙ 提交于 2020-01-04 05:29:27
问题 What prevents me from constantly using the async/wait pattern in my .net code is that once you create an async method the async keyword tends to spread thru my code forcing me to make all methods async. Is there a pattern to stop this efficiently? 回答1: What prevents me from constantly using the async/wait pattern in my .net code is that once you create an async method the async keyword tends to spread thru my code forcing me to make all methods async. Is there a pattern to stop this

Scheduling an async function using the Schedule library. (Using discord.py rewrite)

心已入冬 提交于 2020-01-04 05:13:11
问题 My last post got incorrectly marked as duplicate. I am not trying to do asyncio.sleep, becuase it is too inaccurate over the space of weeks. I need the schedule library. I found a similar thread: How can I run an async function using the schedule library? This did not help me, or the OP of the thread. I ended up with the same error as him, which he stated in a comment of the only answer. My goal is to run a function (which must be async) every x seconds. (Set to seconds for testing purposes.

Parallelizing a for loop with stepping in .net 4.5

試著忘記壹切 提交于 2020-01-04 04:12:21
问题 I have a function that looks something like the code below. Its purpose is to take triangle facets one at a time from an array of points where each three points is one facet, and tessellate them, replacing the facet with a list of smaller facets whose side lengths don't exceed nodeSize. Naturally, this function is time consuming for any realistic facet mesh. I'd like to refactor it to use some coarse parallelization. However, Parallel.For doesn't appear to have a way to step through indices

Find awaitable methods in code with Visual Studio

只谈情不闲聊 提交于 2020-01-04 01:50:50
问题 I have a problem where async methods are being called in the code without await in front of it. Is there a way to find all the awaitable methods that do not have await ? Edit - I'm particularly concerned with the scenario where multiple async methods are being called (ignoring the return values), but only one has await which is enough to make Visual Studio not warn about it. 回答1: If you use ReSharper and turn solution-wide analysis on, your methods that are returning tasks that are not being

async/await clarity, with sleep example

╄→尐↘猪︶ㄣ 提交于 2020-01-04 01:44:16
问题 I am trying to get hang of async/await with below implementation but it is not working as expected public static async sleep(ms: number): Promise<void> { await Utilities._sleep(ms); } private static _sleep(ms: number): Promise<{}> { return new Promise((resolve: Function) => setTimeout(resolve, ms)); } _sleep will resolve promise after n milliseconds, and await should sleep till that time.. but below test of mine is failing it("should sleep for 500 ms", ()=> { const date1 = (new Date())

Accessing the await-ed value inline (JS)?

南楼画角 提交于 2020-01-03 19:14:31
问题 First I wrote something like this: (async function() { const value = await Promise.resolve({a: 3}).a console.log(value) })().catch(console.error); But I quickly came to the conclusion that that's not going to work since we're gonna be awaiting the a property of what Promise.resolve returns, which is undefined. So I tried to wrap my await call into parenthesis: (async function() { const value = await(Promise.resolve({a: 3})).a console.log(value) })().catch(console.error); Which didn't work

How to display data on UI by calling API in Xamarin

拥有回忆 提交于 2020-01-03 18:00:35
问题 I am kinda new to Xamarin and developing an iOS app, I need to call an API and bind the response data to view have used MVVM pattern. Here is my ViewModel Code: public class PersonalDetailModel : BaseViewModel { private PersonalDetails personalDetails { get; set; } public Command LoadCommand; public PersonalDetailModel() { LoadCommand = new Command(async () => await GetPersonalDetais()); } public String City { get { return personalDetails.city; } set { personalDetails.city = value; } } public

Python 3.5 async for blocks the ioloop

て烟熏妆下的殇ゞ 提交于 2020-01-03 17:35:54
问题 I have a simple aiohttp-server with two handlers. First one does some computations in the async for loop. Second one just returns text response. not_so_long_operation returns 30-th fibonacci number with the slowest recursive implementation, which takes something about one second. def not_so_long_operation(): return fib(30) class arange: def __init__(self, n): self.n = n self.i = 0 async def __aiter__(self): return self async def __anext__(self): i = self.i self.i += 1 if self.i <= self.n:

How do I catch thrown errors with async / await?

六眼飞鱼酱① 提交于 2020-01-03 17:12:37
问题 Here's some code: import 'babel-polyfill' async function helloWorld () { throw new Error ('hi') } helloWorld() I also went deep and tried this as well: import 'babel-polyfill' async function helloWorld () { throw new Error ('hi') } async function main () { try { await helloWorld() } catch (e) { throw e } } main() and: import 'babel-polyfill' async function helloWorld () { throw new Error ('hi') } try { helloWorld() } catch (e) { throw e } This works: import 'babel-polyfill' async function