Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/toyobayashi/node-addon-api-coroutine


https://github.com/toyobayashi/node-addon-api-coroutine

Last synced: 2 days ago
JSON representation

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
})
```