Skip to content

Commit 28bf014

Browse files
committed
feat: support async fn
1 parent 6042829 commit 28bf014

File tree

8 files changed

+22
-5
lines changed

8 files changed

+22
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ version = "0.1.0"
1010
crate-type = ["cdylib"]
1111

1212
[dependencies]
13-
napi = "3.0.0"
13+
napi = { version = "3", features = ["async"] }
1414
napi-derive = "3.0.0"
1515

1616
[build-dependencies]

__test__/index.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import test from 'ava'
22

3-
import { plus100 } from '../index.js'
3+
import { plus100, plus100Async } from '../index.js'
44

5-
test('sync function from native code', (t) => {
5+
test('sync function from native code', async (t) => {
66
const fixture = 42
77
t.is(plus100(fixture), fixture + 100)
8+
t.is(await plus100Async(fixture), fixture + 100)
89
})

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/* auto-generated by NAPI-RS */
22
/* eslint-disable */
33
export declare function plus100(input: number): number
4+
5+
export declare function plus100Async(input: number): Promise<number>

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,4 @@ if (!nativeBinding) {
394394

395395
module.exports = nativeBinding
396396
module.exports.plus100 = nativeBinding.plus100
397+
module.exports.plus100Async = nativeBinding.plus100Async

package-template.wasi-browser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ const {
5757
})
5858
export default __napiModule.exports
5959
export const plus100 = __napiModule.exports.plus100
60+
export const plus100Async = __napiModule.exports.plus100Async

package-template.wasi.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,4 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
109109
})
110110
module.exports = __napiModule.exports
111111
module.exports.plus100 = __napiModule.exports.plus100
112+
module.exports.plus100Async = __napiModule.exports.plus100Async

simple-test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
const { plus100 } = require('./index')
22

3-
console.assert(plus100(0) === 100, 'Simple test failed')
3+
async function test() {
4+
console.assert(plus100(0) === 100, 'Simple test failed')
45

5-
console.info('Simple test passed')
6+
console.assert(await plus100Async(0), 'Simple test failed')
7+
8+
console.info('Simple test passed')
9+
}
10+
11+
test()

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ use napi_derive::napi;
66
pub fn plus_100(input: u32) -> u32 {
77
input + 100
88
}
9+
10+
#[napi]
11+
pub async fn plus_100_async(input: u32) -> u32 {
12+
input + 100
13+
}

0 commit comments

Comments
 (0)