napi-rs/test_module/index.js

40 lines
838 B
JavaScript
Raw Normal View History

const testModule = require('./index.node')
2020-02-19 00:05:13 +09:00
function testSpawn() {
console.log('=== Test spawning a future on libuv event loop')
return testModule.testSpawn()
}
function testThrow() {
console.log('=== Test throwing from Rust')
try {
testModule.testThrow()
console.error('Expected function to throw an error')
process.exit(1)
2020-02-19 00:05:13 +09:00
} catch (e) {
console.error(e)
2020-02-19 00:05:13 +09:00
}
}
function testSpawnThread(n) {
console.info('=== Test spawn task to threadpool')
return testModule.testSpawnThread(n)
}
2020-02-19 00:05:13 +09:00
const future = testSpawn()
future
.then((value) => {
console.info(`${value} from napi`)
testThrow()
})
.then(() => testSpawnThread(20))
.then((value) => {
console.assert(value === 6765)
console.info('=== fibonacci result', value)
})
.catch((e) => {
2020-02-19 00:05:13 +09:00
console.error(e)
process.exit(1)
})