napi-rs/examples/napi/src/async.rs
2022-01-23 23:04:31 +08:00

23 lines
511 B
Rust

use futures::prelude::*;
use napi::bindgen_prelude::*;
use napi::tokio::{self, 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
}
#[napi]
async fn async_multi_two(arg: u32) -> Result<u32> {
tokio::task::spawn(async move { Ok(arg * 2) })
.await
.unwrap()
}