Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/toyobayashi/node-addon-api-coroutine
https://github.com/toyobayashi/node-addon-api-coroutine
Last synced: 2 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/toyobayashi/node-addon-api-coroutine
- Owner: toyobayashi
- License: mit
- Created: 2024-06-04T10:34:41.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-06-06T10:03:07.000Z (5 months ago)
- Last Synced: 2024-10-11T20:58:38.326Z (27 days ago)
- Language: C++
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# node-addon-api-coroutine
# Quick Start
```bash
npm install node-addon-api node-addon-api-coroutine
```Create `binding.gyp`
```gyp
{
"targets": [
{
'target_name': 'binding',
'dependencies': [
"naaco::CoPromise CoroutineFunction(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Value async_function = info[0];
if (!async_function.IsFunction()) {
NAPI_CO_THROW(Napi::Error::New(env, "not async function"), Napi::Value());
}
Napi::Value result = co_await async_function.As()({});
co_return result;
}Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set("coroutineFunction", Function::New(env, CoroutineFunction));
return exports;
}NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
```Create `binding.js`
```js
const { coroutineFunction } = require('./build/Release/binding.node')coroutineFunction(function () {
return new Promise((resolve) => {
setTimeout(() => {
resolve(42)
}, 1000)
})
}).then(result => {
console.log(result) // 42
})
```