Merge pull request #838 from napi-rs/fix-async-fn
fix(napi-derive): missing move in async fn
This commit is contained in:
commit
c92c5534c8
6 changed files with 15 additions and 1 deletions
|
@ -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
|
||||
})
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
export function getNums(): Array<number>␊
|
||||
export function sumNums(nums: Array<number>): number␊
|
||||
export function readFileAsync(path: string): Promise<Buffer>␊
|
||||
export function asyncMultiTwo(arg: number): Promise<number>␊
|
||||
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␊
|
||||
|
|
Binary file not shown.
|
@ -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)
|
||||
|
|
1
examples/napi/index.d.ts
vendored
1
examples/napi/index.d.ts
vendored
|
@ -2,6 +2,7 @@ export function getWords(): Array<string>
|
|||
export function getNums(): Array<number>
|
||||
export function sumNums(nums: Array<number>): number
|
||||
export function readFileAsync(path: string): Promise<Buffer>
|
||||
export function asyncMultiTwo(arg: number): Promise<number>
|
||||
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
|
||||
|
|
|
@ -14,3 +14,10 @@ async fn read_file_async(path: String) -> Result<Buffer> {
|
|||
})
|
||||
.await
|
||||
}
|
||||
|
||||
#[napi]
|
||||
async fn async_multi_two(arg: u32) -> Result<u32> {
|
||||
tokio::task::spawn(async move { Ok(arg * 2) })
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue