async-await

Task based vs. thread based Watchdog - but async needed

断了今生、忘了曾经 提交于 2021-01-28 06:13:32
问题 We're using watchdogs to determine whether a connected system is still alive or not. In the previous code we used TCP directly and treated the watchdog in a separate thread. Now is a new service used that provides it's data using gRPC. For that we tried using the async interface with tasks but a task based watchdog will fail. I wrote a small DEMO that abstracts the code and illustrates the problem. You can switch between task based watchdog and thread based watchdog by commenting out line 18

Use async function when consumer doesn't expect a promise

拈花ヽ惹草 提交于 2021-01-28 05:29:38
问题 I'm currently struggling to make an interop call work because I can't synchronously wait for a Promise in javascript. Before you think about closing this as duplicate, note that I have done some searching and I understand that it says everywhere it's not possible to synchronously wait for a Promise. Once async, always async. Source(s): How to wait for a JavaScript Promise to resolve before resuming function?, How can I synchronously determine a JavaScript Promise's state?, Call An

How to test an async function in an event listener with Jest?

ぃ、小莉子 提交于 2021-01-28 04:26:29
问题 I have an event listener that runs an asynchronous function, and removes some elements from the DOM after completion: async function fetchAndRemove() { try { const response = await fetch('/endpoint-that-returns-json') const result = await response.json() if (result.status === 'Success') { document.querySelector('.my-element').remove() } } catch (error) { console.log(error) } } function setupListeners () { const button = document.querySelector('.my-button') button.addEventListener('click', ()

Axios returns promise pending

只愿长相守 提交于 2021-01-28 04:00:50
问题 i want this function to return either true or false, instead I get /** * Sends request to the backend to check if jwt is valid * @returns {boolean} */ const isAuthenticated = () => { const token = localStorage.getItem('jwt'); if(!token) return false; const config = {headers : {'x-auth-token' : token}}; const response = axios.get('http://localhost:8000/user' , config) .then(res => res.status === 200 ? true : false) .catch(err => false); return response; } export default isAuthenticated; I

A simpler way of ignoring specific types of exceptions when awaiting a Task

邮差的信 提交于 2021-01-28 02:24:18
问题 When awaiting a Task I would like to have an easy way to ignore specific types of exceptions, like OperationCanceledException or TimeoutException or whatever. I got the idea of writing an extension method that could wrap my Task , and suppress the Exception type I would give it as argument. So I wrote this one: public static async Task Ignore<TException>(this Task task) where TException : Exception { try { await task; } catch (Exception ex) { if (ex is TException) return; throw; } } I can use

Wait for a task (or tasks) to reach certain milestone, the async/await way

柔情痞子 提交于 2021-01-28 02:01:41
问题 There are tons of examples of how to wait for a thread to exit, but sometimes i need to wait just until several threads are "ready" or have reached certain milestone. After reading: What's the proper way to wait for a .NET thread to start up? and http://www.albahari.com/threading/ If i have understood correctly: Wait for one Child Task to be ready : //Main Method var wh = new AutoResetEvent(false); Task childTask = Task.Run(() => ChildTask(wh); wh.WaitOne(); //Child private void ChildTask

Axios returns promise pending

旧时模样 提交于 2021-01-28 00:59:33
问题 i want this function to return either true or false, instead I get /** * Sends request to the backend to check if jwt is valid * @returns {boolean} */ const isAuthenticated = () => { const token = localStorage.getItem('jwt'); if(!token) return false; const config = {headers : {'x-auth-token' : token}}; const response = axios.get('http://localhost:8000/user' , config) .then(res => res.status === 200 ? true : false) .catch(err => false); return response; } export default isAuthenticated; I

Check if a js function is called using await?

笑着哭i 提交于 2021-01-27 23:13:48
问题 I have a checkPermission function in which I pull session, reach database and confirm permission. If the action is not permitted, the function throws. await checkPermission("USER_READ"); // This will throw if not permitted. // Do permission specific stuff. Problem is if I forget await, it will not wait and move to unpermitted code. Can I programatically check in checkPermission function if await is used so I can throw if await is not used? Or is there any other way I can enforce await for a

C#, Calling async method inside OnStart of Windows service

主宰稳场 提交于 2021-01-27 21:18:45
问题 I am developing a windows service that is able to receive socket connection, so in the OnStart method: protected override void OnStart(string[] args) { start(); } The start function looks like this: public async void Start() { //initialization things ... ... TcpListener listener = new TcpListener(IPAddress.Any, port); listener.Start(); while(true) { TcpClient client = await listener.AcceptTcpClientAsync().ConfigureAwait(false); ... } ... } The problem is that no connection is accepted, while

Polly CircuitBreakerAsync is not working as I expect

早过忘川 提交于 2021-01-27 21:17:17
问题 I'm just trying out the Polly CircuitBreakerAsync and it's not working as I expect. What am I doing wrong here? I expect the code below to complete and say the circuit is still closed. using Polly; using System; using System.Threading.Tasks; public class Program { public static void Main(string[] args) { MainAsync(args).GetAwaiter().GetResult(); } static async Task MainAsync(string[] args) { var circuitBreaker = Policy .Handle<Exception>() .CircuitBreakerAsync( 3, //