From 5bc098144cc6a2b6af8f22bd3bbe73063a2eb2f7 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 16 May 2023 11:05:37 +0800 Subject: [PATCH] chore(example): add callback return promise (#1590) --- .../__snapshots__/typegen.spec.ts.md | 2 ++ .../__snapshots__/typegen.spec.ts.snap | Bin 3966 -> 3987 bytes examples/napi/__tests__/values.spec.ts | 8 ++++++++ examples/napi/index.d.ts | 2 ++ examples/napi/src/callback.rs | 14 +++++++++++++- 5 files changed, 25 insertions(+), 1 deletion(-) 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 5ac96f15313bb5a4af445e000cd1cf384300ea41..910b4e9578771a37d0a117b528bce26498606f90 100644 GIT binary patch literal 3987 zcmV;E4{Y#3RzVO->2yZl`VUZdI_7y~#gbf4da>*|og_z&^)kA1 zos8>=c_i)*7LU9TpkzhSO!E)=xnDZTFYHg~E?!8G1j&=5CUH!HyNd;|c=iRjpQn>3 z3C{UXzfi$ML_hw8&yy?)$&AJ{^#vtClBP5eL|iQ?3F(wfSwtC+{UuNe{?pH4=nMbb zFZX`;*&jZ`e}41(&;Iz^zkd1o1L9{Qd1wTMWO7B02d9S*&qn0I=WFnt@`%OaVaT{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awn', 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,