diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index f7cfbfd9..2d7ed1a8 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -256,6 +256,8 @@ Generated by [AVA](https://avajs.dev). ␊ export function callbackReturnPromise(functionInput: () => T | Promise, callback: (err: Error | null, result: T) => void): T | Promise␊ ␊ + export function callbackReturnPromiseAndSpawn(jsFunc: (arg0: string) => Promise): Promise␊ + ␊ export function callLongThreadsafeFunction(callback: (...args: any[]) => any): void␊ ␊ export function callThreadsafeFunction(callback: (...args: any[]) => any): void␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 5ac96f15..910b4e95 100644 Binary files a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap and b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap differ diff --git a/examples/napi/__tests__/values.spec.ts b/examples/napi/__tests__/values.spec.ts index f4f63705..4d15cb67 100644 --- a/examples/napi/__tests__/values.spec.ts +++ b/examples/napi/__tests__/values.spec.ts @@ -104,6 +104,7 @@ import { CssStyleSheet, asyncReduceBuffer, callbackReturnPromise, + callbackReturnPromiseAndSpawn, returnEitherClass, eitherFromOption, eitherFromObjects, @@ -348,6 +349,13 @@ test('callback function return Promise', async (t) => { t.deepEqual(cbSpy.args, [['42']]) }) +test('callback function return Promise and spawn', async (t) => { + const finalReturn = await callbackReturnPromiseAndSpawn((input) => + Promise.resolve(`${input} world`), + ) + t.is(finalReturn, 'Hello world 😼') +}) + 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 5c86600a..63f523ab 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -246,6 +246,8 @@ export interface C { export function callbackReturnPromise(functionInput: () => T | Promise, callback: (err: Error | null, result: T) => void): T | Promise +export function callbackReturnPromiseAndSpawn(jsFunc: (arg0: string) => Promise): Promise + export function callLongThreadsafeFunction(callback: (...args: any[]) => any): void export function callThreadsafeFunction(callback: (...args: any[]) => any): void diff --git a/examples/napi/src/callback.rs b/examples/napi/src/callback.rs index 069ceb2a..87156629 100644 --- a/examples/napi/src/callback.rs +++ b/examples/napi/src/callback.rs @@ -1,4 +1,4 @@ -use std::env; +use std::{env, format}; use napi::{ bindgen_prelude::*, @@ -81,6 +81,18 @@ fn callback_return_promise Result>( } } +#[napi(ts_return_type = "Promise")] +pub fn callback_return_promise_and_spawn Result>>( + env: Env, + js_func: F, +) -> napi::Result { + let promise = js_func("Hello".to_owned())?; + env.spawn_future(async move { + let resolved = promise.await?; + Ok::(format!("{} 😼", resolved)) + }) +} + #[napi] pub fn capture_error_in_callback Result<()>, E: Fn(Error) -> Result<()>>( cb1: C,