This is a C++ Node Addon; but after some investigation it might not be a
node-level thing...

I have a event loop that gets an event, which when triggered calls a JS
function.  The JS function returns a promise.  The C++ code needs to just
attach .then and .catch events to the promise, and get called when it is
resolved.  (In this case, C++ is just consuming a promise from JS).

What I get when I add my callbacks (which itself works) and return to the
event worker loop is, later, when resolved...

```

Error from thread: TypeError: Method Promise.prototype.then called on
incompatible receiver undefined
    at then (<anonymous>)
/usr/bin/node[232537]: ../src/debug_utils.cc:289:void
node::CheckedUvLoopClose(uv_loop_t*): Assertion `0 && "uv_loop_close()
while having open handles"' failed.
 1: 0x555555b21041 node::Abort() [/usr/bin/node]

```

This is the API of A Promise...
https://v8docs.nodesource.com/node-12.0/d3/d8f/classv8_1_1_promise.html#aca2ce1ed00df2f653af444c8f186f3db


it has a Then() function; which I think is to add callbacks?

```
static void acceptResolved(const v8::FunctionCallbackInfo<Value>& args ) {
printf( "Reject called on promise...");
}
static void acceptRejected(const v8::FunctionCallbackInfo<Value>& args ) {
printf( "Reject called on promise...");
}


//...... This is the call into JS, and getting the result as a promise....

MaybeLocal<Value> result = cb->Call( context,
eventMessage->_this->_this.Get( isolate ), 1, argv );
if( !result.IsEmpty() ) {
Local<Value> ret = result.ToLocalChecked();
Local<Promise> retPro = ret.As<Promise>();
if( !retPro.IsEmpty() ){
wssiInternal->acceptEventMessage = eventMessage;
wssiInternal->acceptPromise.Reset( isolate, retPro );
Local<Function> promiseResolved =
v8::Function::New(isolate->GetCurrentContext(), acceptResolved, wssi
).ToLocalChecked();
Local<Function> promiseRejected =
v8::Function::New(isolate->GetCurrentContext(), acceptRejected, wssi
).ToLocalChecked();

//Then (Local< Context > context, Local< Function > on_fulfilled, Local<
Function > on_rejected)
MaybeLocal<Promise> retval = retPro->Then( context, promiseResolved,
promiseRejected );
//  TypeError: Method Promise.prototype.then called on incompatible
receiver undefined at then (<anonymous>)

// don't set done, keep blocking the acceptor...
continue;
}
}

```

There wasn't a specific addon example with this; and NAPI just provides
creation and resolution of promises, not catching the resolution of them.
https://github.com/nodejs/node-addon-examples/issues/85#issuecomment-558022938

Someone suggested that I modify the above....

```
// get and save the prototype then and catch functions; should be constant?
// save them in a Persistent<Function> for later...
if( c->promiseThen.IsEmpty()){
   Local<Function> cons = Local<Function>::Cast(
         retPro->Get( context, String::NewFromUtf8( isolate, "then",
NewStringType::kNormal ).ToLocalChecked() ).ToLocalChecked());
   c->promiseThen.Reset( isolate, cons );
}
if( c->promiseCatch.IsEmpty()){
   Local<Function> cons = Local<Function>::Cast(
         retPro->Get( context, String::NewFromUtf8( isolate, "catch",
NewStringType::kNormal ).ToLocalChecked() ).ToLocalChecked());
   c->promiseCatch.Reset( isolate, cons );
}

// and then use...
// ---  MaybeLocal<Promise> retval = retPro->Then( context,
promiseResolved, promiseRejected );
// +++ ...
    Local<Value> argv[1] = { promiseResolved };
   Local<Value> result = c->promiseThen.Get( isolate )->Call( context,
retPro, 1, argv ).ToLocalChecked();
   argv[0] = { promiseRejected };
   result = c->promiseCatch.Get( isolate )->Call( context, retPro, 1, argv
).ToLocalChecked();
// not that I need result... or to local checked or...

```


Why can't I just use the Then() function?

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-users/CAA2GJqVe-6cErvkkf31ZZ3rD_OG8r5LeMwjDkQiZ3fYy9CXNew%40mail.gmail.com.

Reply via email to