Skip to main content

TaskCallback

Type Alias TaskCallback 

Source
pub type TaskCallback = Box<dyn Fn(Vec<u8>) + Send>;
Available on crate feature embassy-runtime only.
Expand description

Callback type used by Embassy tasks to resolve a method call.

The callback should be invoked exactly once with the method’s return data. Usually right after your task is finished.

§Panics

Usually you would get this function as the second parameter of the task functions (i.e. the ones marked with #[embassy_executor::task]) that you brought into the embassy_method_accessor!.

Inside, that function calls OnceLock::init(), right followed by an .unwrap(). So this function panics when it is called multiple times. This usually indicates a tragic logic failure.

§Example

use embassy_time::Timer;
use pk_command::embassy_adapter::TaskCallback;

#[embassy_executor::task]
async fn async_echo(param: Vec<u8>, callback: TaskCallback) {
    // You get the callback function^^^^^^^^ here.

    Timer::after_millis(10).await;
    callback(param);
}

Aliased Type§

pub struct TaskCallback(/* private fields */);