nodejs / nodejs/nan

Assigning Callbacks Within Callbacks

Open
#908 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
3.4k
Forks
531
Avg merge
21m
Merged PRs (30d)
1

Description

Hi,

I have a C++ library with an API call which will take in a callback.
That callback will return a reference to another callback that is to be set while in the first callback.
I'm trying to find a way to do this.

Assigning the first callback is straightforward from js to c++

Nan::MaybeLocal<v8::Function> function_ = Nan::To<v8::Function>(info[0]);
Nan::Callback* callback_ = new Nan::Callback(function_.ToLocalChecked());

and then calling the callback

Nan::HandleScope scope;
Nan::AsyncResource resource("async_connect::on_connect");

v8::Local<v8::Object> target = Nan::New<v8::Object>();

resource.runInAsyncScope(target, callback_->GetFunction(), 0, nullptr);

this all works fine ( no arguments provided with the callback ).

So I am trying to figure out how I can provide an argument to that callback, from c++ into js, that would allow storing another callback.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the Nan::Callback setup and the resource.runInAsyncScope call shown in the issue. Determine how an argument can be passed from the C++ callback invocation into JavaScript and how that argument could represent another callback. Done should include a documented, working approach for assigning the nested callback.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, javascript, node.js
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.