diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index 2f828ffc..a5b869a2 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -33,7 +33,7 @@ impl TryToTokens for NapiFn { quote! { Ok(#receiver(#(#arg_names),*).await) } }; quote! { - execute_tokio_future(env, async { #call }, |env, #receiver_ret_name| { + execute_tokio_future(env, async move { #call }, |env, #receiver_ret_name| { #ret }) } diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index a5412e72..4f2bb9cf 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -12,6 +12,7 @@ Generated by [AVA](https://avajs.dev). export function getNums(): Array␊ export function sumNums(nums: Array): number␊ export function readFileAsync(path: string): Promise␊ + export function asyncMultiTwo(arg: number): Promise␊ export function getCwd(callback: (arg0: string) => void): void␊ export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void␊ export function eitherStringOrNumber(input: string | number): number␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 0071ef33..d92c080a 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index d1548995..479e8810 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -34,6 +34,7 @@ import { either4, withoutAbortController, withAbortController, + asyncMultiTwo, } from '../' test('number', (t) => { @@ -163,6 +164,10 @@ test('async', async (t) => { await t.throwsAsync(() => readFileAsync('some_nonexist_path.file')) }) +test('async move', async (t) => { + t.is(await asyncMultiTwo(2), 4) +}) + test('either', (t) => { t.is(eitherStringOrNumber(2), 2) t.is(eitherStringOrNumber('hello'), 'hello'.length) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index caa26c4f..ab2e701d 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -2,6 +2,7 @@ export function getWords(): Array export function getNums(): Array export function sumNums(nums: Array): number export function readFileAsync(path: string): Promise +export function asyncMultiTwo(arg: number): Promise export function getCwd(callback: (arg0: string) => void): void export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void export function eitherStringOrNumber(input: string | number): number diff --git a/examples/napi/src/async.rs b/examples/napi/src/async.rs index 778c53ce..76ea50bd 100644 --- a/examples/napi/src/async.rs +++ b/examples/napi/src/async.rs @@ -14,3 +14,10 @@ async fn read_file_async(path: String) -> Result { }) .await } + +#[napi] +async fn async_multi_two(arg: u32) -> Result { + tokio::task::spawn(async move { Ok(arg * 2) }) + .await + .unwrap() +}