async-await

What is the random factor in node v10 event loop?

对着背影说爱祢 提交于 2020-05-26 09:31:32
问题 My question is about nodejs event loop Consider this code (async () => { let val = 1 const promise = new Promise(async resolve => { resolve() await new Promise(async r => { setTimeout(r) }) await promise val = 2 }) await promise await new Promise(resolve => setTimeout(resolve)) console.log(val) })() With node 10.20.1 (latest version of node 10) for ((i = 0; i < 30; i++)); do /opt/node-v10.20.1-linux-x64/bin/node race-timeout.js; done With node 12.0.0 (first version of node 12) for ((i = 0; i

Using for await…of with synchronous iterables

偶尔善良 提交于 2020-05-25 06:46:27
问题 MDN says for await...of has two use-cases: The for await...of statement creates a loop iterating over async iterable objects as well as on sync iterables,... I was previously aware of the former: async iterables using Symbol.asyncIterator . But I am now interested in the latter: synchronous iterables. The following code iterates over a synchronous iterable - an array of promises. It appears to block progess on the fulfilment of each promise. async function asyncFunction() { try { const happy

Using for await…of with synchronous iterables

人走茶凉 提交于 2020-05-25 06:45:03
问题 MDN says for await...of has two use-cases: The for await...of statement creates a loop iterating over async iterable objects as well as on sync iterables,... I was previously aware of the former: async iterables using Symbol.asyncIterator . But I am now interested in the latter: synchronous iterables. The following code iterates over a synchronous iterable - an array of promises. It appears to block progess on the fulfilment of each promise. async function asyncFunction() { try { const happy

react-native async function returns promise but not my json data?

妖精的绣舞 提交于 2020-05-24 18:15:47
问题 I'm learning react-native, and I'm running into an issue. Why does getting data on return from an async function return a promise, but in the async function itself, it correctly returns an array of objects? On componentDidMount() , I call my async function which in turn does a fetch to an api url: componentDidMount() { let data = this.getData(); console.log(data); // <-- Promise {_40: 0, _65: 0, _55: null, _72: null} this.setState({ dataSource:this.state.dataSource.cloneWithRows(data), }) }

How to return AggregateException from async method

余生长醉 提交于 2020-05-24 04:07:09
问题 I got an async method working like an enhanced Task.WhenAll . It takes a bunch of tasks and returns when all are completed. public async Task MyWhenAll(Task[] tasks) { ... await Something(); ... // all tasks are completed if (someTasksFailed) throw ?? } My question is how do I get the method to return a Task looking like the one returned from Task.WhenAll when one or more tasks has failed? If I collect the exceptions and throw an AggregateException it will be wrapped in another

How to create a Task which always yields?

ぐ巨炮叔叔 提交于 2020-05-23 13:24:43
问题 In contrast to Task.Wait() or Task.Result , await ’ing a Task in C# 5 prevents the thread which executes the wait from lying fallow. Instead, the method using the await keyword needs to be async so that the call of await just makes the method to return a new task which represents the execution of the async method. But when the await ’ed Task completes before the async method has received CPU time again, the await recognizes the Task as finished and thus the async method will return the Task

How can I watch and wait until file is created? [duplicate]

旧街凉风 提交于 2020-05-22 09:58:11
问题 This question already has answers here : How to wait for signal in WinForms while also listening to events? (3 answers) Closed 12 days ago . I'm using FileSystemWatcher. I'm calling the WatchDirectory from a button click event. Then i want assign to label6.Text once the file is busy to display "busy" and when the file is not busy any more to display "not busy". And using async i'm not sure if it's the right way here. This wait the methods are i'm getting errors. On WatchDirectory i'm getting

Get data using await async without try catch

风格不统一 提交于 2020-05-21 17:19:23
问题 I am trying to use await-async without try-catch for this: const getUsers = async (reject, time) => ( new Promise((resolve, reject) => { setTimeout(() => { if (reject) { reject(....) } resolve(.....); }, time); }) ); module.exports = { getUsers , }; With try-catch block it looks like this: const { getUsers } = require('./users'); const users = async () => { try { const value = await getUsers(1000, false); ..... } catch (error) { ..... } } users(); How can I write the same code without using

Sequential version of asyncio.gather

99封情书 提交于 2020-05-17 06:22:08
问题 I tried to create a method similar to asyncio.gather, but which will execute the list of tasks sequentially and not asynchronously: async def in_sequence(*tasks): """Executes tasks in sequence""" for task in tasks: await task Next this method was supposed to be used like this: async def some_work(work_name): """Do some work""" print(f"Start {work_name}") await asyncio.sleep(1) if raise_exception: raise RuntimeError(f"{work_name} raise an exception") print(f"Finish {work_name}") async def main

Where does Promise executor callback live when asynchronous code is being run?

爱⌒轻易说出口 提交于 2020-05-17 03:04:15
问题 A Promise constructor function can take a executor callback function and this question is about where does that callback function live in execution space when the executor callback function has asynchronous code. DETAILS : A Promise object represents a value that may not be available yet, but will be resolved at some point in the future. It allows you to write asynchronous code like making a call to a remote web service, you will create a Promise object which represents the data that will be