async fn

async fn

You can do a lot of async/multi-threaded work with AsyncTask and ThreadsafeFunction, but sometimes you may want to use the crates from the Rust async ecosystem directly.

NAPI-RS supports the tokio runtime by default. If you await a tokio future in async fn, NAPI-RS will execute it in the tokio runtime and convert it into a JavaScript Promise.

use futures::prelude::*;
use napi::bindgen_prelude::*;
use tokio::fs;

async fn read_file_async(path: String) -> Result<Buffer> {
    .map(|r| match r {
      Ok(content) => Ok(content.into()),
      Err(e) => Err(Error::new(
        format!("failed to read file, {}", e),


export function readFileAsync(path: string): Promise<Buffer>