Skip to content

Instantly share code, notes, and snippets.

@zwang
Forked from jupp0r/Demo.cpp
Created December 20, 2019 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zwang/6d3f805a0f28ae351888ae11bc77127d to your computer and use it in GitHub Desktop.
Save zwang/6d3f805a0f28ae351888ae11bc77127d to your computer and use it in GitHub Desktop.
C++ function returning an ES6 promise
#include <node.h>
#include "Test.h"
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void Method(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto resolver = v8::Promise::Resolver::New(isolate);
args.GetReturnValue().Set(resolver->GetPromise());
resolver->Resolve(String::NewFromUtf8(isolate, "world"));
}
void init(Local<Object> exports) { NODE_SET_METHOD(exports, "hello", Method); }
NODE_MODULE(addon, init)
} // namespace demo
let addon = require('./build/Release/webidl-poc.node')
addon.hello().then(msg => console.log(msg))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment