napi-rs/examples/napi/src/async.rs

17 lines
360 B
Rust
Raw Normal View History

2021-10-25 01:00:31 +09:00
use futures::prelude::*;
use napi::bindgen_prelude::*;
use tokio::fs;
#[napi]
async fn read_file_async(path: String) -> Result<Buffer> {
2021-10-25 01:00:31 +09:00
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),
)),
2021-10-25 01:00:31 +09:00
})
.await
}