async-await

How can I alert an asynchronous function that another instance of the same function has fired?

感情迁移 提交于 2020-03-04 18:59:12
问题 I am wanting to create an alert inside my function which tracks if any other instances of the same function have fired during a 15 second period. this is what I have so far: bar = 0; async function Counting() { bar += 1; foo = bar; new Promise(resolve => setTimeout(resolve, 5000)); if (bar == foo) { //Do something } else { return; } } I'm using bar as a global counter and foo as a function instance counter, but for some reason all instances of the function update at the same time. How can I

How can I alert an asynchronous function that another instance of the same function has fired?

▼魔方 西西 提交于 2020-03-04 18:58:30
问题 I am wanting to create an alert inside my function which tracks if any other instances of the same function have fired during a 15 second period. this is what I have so far: bar = 0; async function Counting() { bar += 1; foo = bar; new Promise(resolve => setTimeout(resolve, 5000)); if (bar == foo) { //Do something } else { return; } } I'm using bar as a global counter and foo as a function instance counter, but for some reason all instances of the function update at the same time. How can I

PutAsync doesn't send request to web api, but fiddler works fine

…衆ロ難τιáo~ 提交于 2020-03-02 04:40:06
问题 I have been trying to figure out what is going wrong for a few hours now and i just can't find what is going wrong. Via the Mvc application the put method doesn't get hit, the request doesn't happen. But when i test it in fiddler the PutMethod in the api works. Hopefully someone can clear things up for me. Also pointers for a better structure or some good documentation are welcome . public void UpdateWerknemerCompetentieDetail(int wnID, int WNC, CompetentieWerknemerDetail detail) { using

How to Unit test ViewModel with async initialization in WPF

别等时光非礼了梦想. 提交于 2020-02-25 02:53:09
问题 I have created a sample WPF MVVM project which I now want to Unit test. The viewmodels load the data asynchronously in the constructor: public class CustomerOverviewViewModel { public CustomerOverviewViewModel() { var t = LoadFullCustomerListAsync(); } public async Task LoadFullCustomerListAsync() { List<BL_acc> customers = await Task.Run(() => // Query from db); } } In WPF, this works like a charm. When I want to create a unit test for this viewmodel, I create the object by calling its

Converting Promises Code to async await and mocking test cases?

送分小仙女□ 提交于 2020-02-24 20:24:52
问题 How to convert the below function which has nested promise and await to just using await or only using promises ? const test = (url, id) => { return new Promise((_resolve, _reject) => { if (isValidUrl(url)) { let storage = new Storage(Indexdb, id); const cae = new valueExtract(url); cae.fetch() .then(data => { new zip(data) .then(obj => obj.getZip()) .then(obj => obj.getList()) .then(list => { return new Promise(async (resolve, reject) => { try { let sI = storage.connection; await Promise.all

Converting Promises Code to async await and mocking test cases?

橙三吉。 提交于 2020-02-24 20:23:49
问题 How to convert the below function which has nested promise and await to just using await or only using promises ? const test = (url, id) => { return new Promise((_resolve, _reject) => { if (isValidUrl(url)) { let storage = new Storage(Indexdb, id); const cae = new valueExtract(url); cae.fetch() .then(data => { new zip(data) .then(obj => obj.getZip()) .then(obj => obj.getList()) .then(list => { return new Promise(async (resolve, reject) => { try { let sI = storage.connection; await Promise.all

Using async/await with old `Future<Item = X, Error = Y>` types [duplicate]

瘦欲@ 提交于 2020-02-24 05:00:06
问题 This question already has an answer here : Is there a way that we can convert from futures 0.1 to the standard library futures? (1 answer) Closed 24 days ago . I have a function in a crate that returns old style futures. Imagine something like: pub fn old_function() -> impl Future<Item = X, Error = Y> ... I want to use this crate in a new codebase where I don't want to mix things too much. How can I keep the new implementation clean and use async/await when calling this old_function ? 回答1:

How to use a Rust async fn that takes a reference as a callback?

喜夏-厌秋 提交于 2020-02-20 07:35:06
问题 async fn returns an anonymous type that implements Future , so if we want to use it as a callback, we need to convert the return value to a trait object. I tried to write an function to do this, but I had some lifetime problems. async fn will return lifetime of all parameters, so the signature of callback also needs to. How can I add the lifetime to the return value of the callback? use futures::future::{Future, FutureExt, LocalBoxFuture}; type Context = (); type AsyncCb = Box<dyn for<'r>

javascript : Async/await in .replace

放肆的年华 提交于 2020-02-19 09:43:11
问题 I am using the async/await function the following way async function(){ let output = await string.replace(regex, async (match)=>{ let data = await someFunction(match) console.log(data); //gives correct data return data }) return output; } But the returned data is an promise object. Just confused about the way it should be implemented in such functions with callback. 回答1: An easy function to use and understand for some async replace : async function replaceAsync(str, regex, asyncFn) { const

OperationCanceledException VS TaskCanceledException when task is canceled

核能气质少年 提交于 2020-02-19 09:35:33
问题 The following code creates a task which is being canceled. await expression (case 1) throws System.OperationCanceledException while synchronous Wait() (case 2) throws System.Threading.Tasks.TaskCanceledException (wrapped in System.AggregateException ). using System; using System.Threading; using System.Threading.Tasks; public class Program { public static void Main() { Program.MainAsync().Wait(); } private static async Task MainAsync() { using(var cancellationTokenSource = new