async-await

How to accept an async function as an argument?

六眼飞鱼酱① 提交于 2020-07-21 11:13:01
问题 I would like to replicate the behavior and ergonomics of taking a closure/function as an argument much like map does: iterator.map(|x| ...) . I've noticed that some library code allows passing in async functionality, but this method doesn't allow me to pass in arguments: pub fn spawn<F, T>(future: F) -> JoinHandle<T> where F: Future<Output = T> + Send + 'static, T: Send + 'static, spawn(async { foo().await }); I'm hoping to do one of the following: iterator.map(async |x| {...}); async fn a(x:

nodejs run async function one after another

泪湿孤枕 提交于 2020-07-21 06:24:58
问题 I'm new to JS/nodejs, so please pardon me if I can't ask to-the-point question. So basically, if I have two async functions, async function init() {...} async function main() {...} How can I make sure to call main() after init() has finished its async requests? Specifically, I want to make use of the module https://www.npmjs.com/package/hot-import whereas on its page, there is a sample code: async function main() { const MODULE_CODE_42 = 'module.exports = () => 42' const MODULE_CODE_17 =

How can one await a result of a boxed future?

蓝咒 提交于 2020-07-21 02:26:25
问题 use futures::{future, Future}; fn test() -> Box<dyn Future<Output = bool>> { Box::new(future::ok::<bool>(true)) } async fn async_fn() -> bool { let result: bool = test().await; return result; } fn main(){ async_fn(); println!("Hello!"); } Playground Error: error[E0277]: the trait bound `dyn core::future::future::Future<Output = bool>: std::marker::Unpin` is not satisfied --> src/main.rs:8:24 | 8 | let result: bool = test().await; | ^^^^^^^^^^^^ the trait `std::marker::Unpin` is not

async/await in MVC controller's action

泄露秘密 提交于 2020-07-17 07:33:39
问题 I have an Index action in ASP.net MVC controller. This action, calls (among other things) a private action that do a count on a SQL table with large set of rows. The returned number will be inserted in a view bag property. public ActionResult Index() { // do things ViewBag.NumberOfRows = NumberOfRows(); return View(); } private string NumberOfRows() { // sql connection and row count return numberOfRows; } This works but I can't see the Index page until everything is executed, even the row

unit testing async task in csharp

柔情痞子 提交于 2020-07-15 08:43:51
问题 I'm new to unit testing and async operations in visual studio/c#. Appreciate any help on this. My Main class class Foo { public async Task<string> GetWebAsync() { using (var client = new HttpClient()) { var response = await client.GetAsync("https://hotmail.com"); return await response.Content.ReadAsStringAsync(); } } } Unit Test [TestMethod] public void TestGet() { Foo foo = new Foo(); foo.GetWebAsync().ContinueWith((k) => { Console.Write(k); Assert.IsNotNull(null, "error"); }); } 回答1: Make

Are a .NET Task thread's resources returned back to the pool temporarily if the thread is waiting on an async operation to finish?

这一生的挚爱 提交于 2020-07-15 06:41:05
问题 I have a TPL Task that does two things. First, it calls a web service. Second, it inserts some data into a database. I have up to 20 Tasks started at one time doing this same thing over and over again. All they do all day is call web services and insert data into a database. I'm fairly new to TPL in .NET. I've done some stuff with background worker processes and async web services. The web service call and the database insert are both blocking calls within the thread the Task is running in. I

Async function returning promise, instead of value

ぃ、小莉子 提交于 2020-07-15 05:10:26
问题 I'm trying to understand how async/await works in conjunction together with promises. Code async function latestTime() { const bl = await web3.eth.getBlock('latest'); console.log(bl.timestamp); // Returns a primitive console.log(typeof bl.timestamp.then == 'function'); //Returns false - not a promise return bl.timestamp; } const time = latestTime(); // Promise { <pending> } Issue As far as I understand, await should be blocking and in the code above it seemingly blocks returning an object bl

Rewrite error catching higher order function to catch async errors?

徘徊边缘 提交于 2020-07-09 19:45:00
问题 Here I have a function that works well for catching sync errors, and doing something with them before re-throwing them. function logExceptions<T extends (...args: any[]) => any>(func: T): (...funcArgs: Parameters<T>) => ReturnType<T> { return (...args: Parameters<T>): ReturnType<T> => { try { return func(...args); } catch (err) { console.log(func.name + " caused an error") throw err; } }; } function syncExample() { throw new Error() } logExceptions(syncExample)(); console.log "syncExample

Rewrite error catching higher order function to catch async errors?

ぃ、小莉子 提交于 2020-07-09 19:44:47
问题 Here I have a function that works well for catching sync errors, and doing something with them before re-throwing them. function logExceptions<T extends (...args: any[]) => any>(func: T): (...funcArgs: Parameters<T>) => ReturnType<T> { return (...args: Parameters<T>): ReturnType<T> => { try { return func(...args); } catch (err) { console.log(func.name + " caused an error") throw err; } }; } function syncExample() { throw new Error() } logExceptions(syncExample)(); console.log "syncExample

Synchronous generator in asyncio

蓝咒 提交于 2020-07-09 14:34:39
问题 I have the following scenario: I have a blocking, synchronous generator I have an non-blocking, async function I would like to run blocking generator (executed in a ThreadPool ) and the async function on the event loop. How do I achieve this? The following function simply prints the output from the generator, not from sleep function. Thanks! from concurrent.futures import ThreadPoolExecutor import numpy as np import asyncio import time def f(): while True: r = np.random.randint(0, 3) time