nodejs/node-addon-examples

napi-asyncworker-example missing scope

rynz opened this issue · 3 comments

rynz commented

void SimpleAsyncWorker::OnOK() {
std::string msg = "SimpleAsyncWorker returning after 'working' " + std::to_string(runTime) + " seconds.";
Callback().Call({Env().Null(), String::New(Env(), msg)});
};

This seems to be the only example that doesn't call:

Napi::HandleScope scope(Env());

Should this example include that too?

@rynz actually, it should be the other way around. Node.js ensures that all scopes are present by the time the OnOK/OnError callbacks get called. Thanks for noticing the discrepancy!

We have additional documentation here: https://github.com/nodejs/node-addon-api/blob/master/doc/object_lifetime_management.md

rynz commented

@gabrielschulhof thank you!

Can someone who is more familiar with this than I am check to see if there is still bad example code in node-addon-api/doc/async_worker.md It appears that the OnOK example is using the Napi::HandleScope when it is not necessary.

Thanks