async fn
💡
Você deve habilitar o recurso async ou tokio_rt no napi
para usar async fn
:
Cargo.toml
[dependencies]
napi = { version = "2", features = ["async"] }
Você pode realizar muitos trabalhos async/multi-threaded com AsyncTask
e ThreadsafeFunction
, mas às vezes você pode querer usar diretamente as crates do ecossistema async do Rust.
NAPI-RS suporta o runtime do tokio
por padrão. Se você await
um future
do tokio em uma async fn
, NAPI-RS o executará no runtime do tokio e o converterá em uma Promise
do JavaScript.
lib.rs
use futures::prelude::*;
use napi::bindgen_prelude::*;
use tokio::fs;
#[napi]
async fn read_file_async(path: String) -> Result<Buffer> {
fs::read(path)
.map(|r| match r {
Ok(content) => Ok(content.into()),
Err(e) => Err(Error::new(
Status::GenericFailure,
format!("failed to read file, {}", e),
)),
})
.await
}
⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️
index.d.ts
export function readFileAsync(path: string): Promise<Buffer>