Skip to content

Instantly share code, notes, and snippets.

@jupp0r
Created March 7, 2017 10:20
Show Gist options
  • Save jupp0r/5f11c0ee2b046b0ab89660ce85ea480e to your computer and use it in GitHub Desktop.
Save jupp0r/5f11c0ee2b046b0ab89660ce85ea480e 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