diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 2a93f83a..3a8c38e5 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -40,6 +40,7 @@ Generated by [AVA](https://avajs.dev). /** napi = { version = 2, features = ["serde-json"] } */␊ export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void␊ export function returnJsFunction(): (...args: any[]) => any␊ + export function callbackReturnPromise(functionInput: () => T | Promise, callback: (err: Error | null, result: T) => void): T | Promise␊ export function dateToNumber(input: Date): number␊ export function chronoDateToMillis(input: Date): number␊ export function chronoDateAdd1Minute(input: Date): Date␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 86e0d3e0..68e1450c 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 dd66079c..3cda09af 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -2,6 +2,7 @@ import { exec } from 'child_process' import { join } from 'path' import test from 'ava' +import { spy } from 'sinon' import { DEFAULT_COST, @@ -89,6 +90,7 @@ import { bufferPassThrough, JsRepo, asyncReduceBuffer, + callbackReturnPromise, } from '../' test('export const', (t) => { @@ -224,6 +226,21 @@ test('return function', (t) => { }) }) +test('function return Promise', async (t) => { + const cbSpy = spy() + await callbackReturnPromise(() => '1', spy) + t.is(cbSpy.callCount, 0) + await callbackReturnPromise( + () => Promise.resolve('42'), + (err, res) => { + t.is(err, null) + cbSpy(res) + }, + ) + t.is(cbSpy.callCount, 1) + t.deepEqual(cbSpy.args, [['42']]) +}) + test('object', (t) => { t.deepEqual(listObjKeys({ name: 'John Doe', age: 20 }), ['name', 'age']) t.deepEqual(createObj(), { test: 1 }) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 87b6a8f3..c03e1ff1 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -30,6 +30,7 @@ export function optionOnly(callback: (arg0?: string | undefined | null) => void) /** napi = { version = 2, features = ["serde-json"] } */ export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void export function returnJsFunction(): (...args: any[]) => any +export function callbackReturnPromise(functionInput: () => T | Promise, callback: (err: Error | null, result: T) => void): T | Promise export function dateToNumber(input: Date): number export function chronoDateToMillis(input: Date): number export function chronoDateAdd1Minute(input: Date): Date diff --git a/examples/napi/src/callback.rs b/examples/napi/src/callback.rs index 14793f61..f92e01ef 100644 --- a/examples/napi/src/callback.rs +++ b/examples/napi/src/callback.rs @@ -53,7 +53,8 @@ fn return_js_function(env: Env) -> Result { #[napi( ts_generic_types = "T", - ts_args_type = "functionInput: () => T | Promise, callback: (err: Error | null, result: T) => void" + ts_args_type = "functionInput: () => T | Promise, callback: (err: Error | null, result: T) => void", + ts_return_type = "T | Promise" )] fn callback_return_promise Result>( env: Env,