Merge pull request #838 from napi-rs/fix-async-fn

fix(napi-derive): missing move in async fn
This commit is contained in:
LongYinan 2021-11-06 22:31:23 +08:00 committed by GitHub
commit c92c5534c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 1 deletions

View file

@ -33,7 +33,7 @@ impl TryToTokens for NapiFn {
quote! { Ok(#receiver(#(#arg_names),*).await) } quote! { Ok(#receiver(#(#arg_names),*).await) }
}; };
quote! { quote! {
execute_tokio_future(env, async { #call }, |env, #receiver_ret_name| { execute_tokio_future(env, async move { #call }, |env, #receiver_ret_name| {
#ret #ret
}) })
} }

View file

@ -12,6 +12,7 @@ Generated by [AVA](https://avajs.dev).
export function getNums(): Array<number> export function getNums(): Array<number>
export function sumNums(nums: Array<number>): number␊ export function sumNums(nums: Array<number>): number␊
export function readFileAsync(path: string): Promise<Buffer> export function readFileAsync(path: string): Promise<Buffer>
export function asyncMultiTwo(arg: number): Promise<number>
export function getCwd(callback: (arg0: string) => void): void␊ export function getCwd(callback: (arg0: string) => void): void␊
export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void␊ export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void␊
export function eitherStringOrNumber(input: string | number): number␊ export function eitherStringOrNumber(input: string | number): number␊

View file

@ -34,6 +34,7 @@ import {
either4, either4,
withoutAbortController, withoutAbortController,
withAbortController, withAbortController,
asyncMultiTwo,
} from '../' } from '../'
test('number', (t) => { test('number', (t) => {
@ -163,6 +164,10 @@ test('async', async (t) => {
await t.throwsAsync(() => readFileAsync('some_nonexist_path.file')) await t.throwsAsync(() => readFileAsync('some_nonexist_path.file'))
}) })
test('async move', async (t) => {
t.is(await asyncMultiTwo(2), 4)
})
test('either', (t) => { test('either', (t) => {
t.is(eitherStringOrNumber(2), 2) t.is(eitherStringOrNumber(2), 2)
t.is(eitherStringOrNumber('hello'), 'hello'.length) t.is(eitherStringOrNumber('hello'), 'hello'.length)

View file

@ -2,6 +2,7 @@ export function getWords(): Array<string>
export function getNums(): Array<number> export function getNums(): Array<number>
export function sumNums(nums: Array<number>): number export function sumNums(nums: Array<number>): number
export function readFileAsync(path: string): Promise<Buffer> export function readFileAsync(path: string): Promise<Buffer>
export function asyncMultiTwo(arg: number): Promise<number>
export function getCwd(callback: (arg0: string) => void): void export function getCwd(callback: (arg0: string) => void): void
export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void
export function eitherStringOrNumber(input: string | number): number export function eitherStringOrNumber(input: string | number): number

View file

@ -14,3 +14,10 @@ async fn read_file_async(path: String) -> Result<Buffer> {
}) })
.await .await
} }
#[napi]
async fn async_multi_two(arg: u32) -> Result<u32> {
tokio::task::spawn(async move { Ok(arg * 2) })
.await
.unwrap()
}