Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to accept an async function as an argument?

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: _) {}
iterator.map(a)
like image 993
Skarlett Avatar asked Mar 17 '20 07:03

Skarlett


People also ask

Can you pass an async function to a Promise?

Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

Can async function have parameters?

An async function (or AyncFunction) in the context of Async is an asynchronous function with a variable number of parameters where the final parameter is a callback.

Can you pass an async function to then?

An async function can have more than one await expression in its scope which can pause the execution inside its function scope. Once the passed Promise has been resolved, it will resume the async function's execution.

How does async function return a value?

To return values from async functions using async-await from function with JavaScript, we return the resolve value of the promise. const getData = async () => { return await axios.

What is an async function?

An async function is a function declared with the async keyword, and the await keyword is permitted within them. The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains. Async functions may also be defined as expressions.

What is the behavior of async/await in JavaScript?

The behavior of async / await is similar to combining generators and promises. Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. For example, the following: async function foo() { return 1 }. ...is equivalent to:

What happens when you pass an async function as a parameter?

The output of the 1st example is: It is not a problem if you simply call your async function as a function expression, but when you pass your async function as a parameter into a function especially a 3rd-party function, you’re passing it into a black box, and you’ll lose control of your async function’s execution.

Do async functions always return a promise?

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. For example, the following: async function foo() { return 1 }


1 Answers

async functions are effectively desugared as returning impl Future. Once you know that, it's a matter of combining existing Rust techniques to accept a function / closure, resulting in a function with two generic types:

use std::future::Future;

async fn example<F, Fut>(f: F)
where
    F: FnOnce(i32, i32) -> Fut,
    Fut: Future<Output = bool>,
{
    f(1, 2).await;
}

This can also be written as

use std::future::Future;

async fn example<Fut>(f: impl FnOnce(i32, i32) -> Fut)
where
    Fut: Future<Output = bool>,
{
    f(1, 2).await;
}
  • How do you pass a Rust function as a parameter?
  • What is the concrete type of a future returned from `async fn`?
  • What is the purpose of async/await in Rust?
  • How can I store an async function in a struct and call it from a struct instance?
  • What is the difference between `|_| async move {}` and `async move |_| {}`
like image 172
Shepmaster Avatar answered Sep 18 '22 17:09

Shepmaster