title: Add node NAPI Function as callback to native library
body: <p>Is it possible to add javascript function as callback to a native library accepting callbacks through NAPI?</p>
  
  <p>Here's an example addon code I have so far.</p>
  
  <pre class="lang-c prettyprint-override"><code>
  Napi::Function *jsCallback;
  void RegisterReadyEvent(const Napi::CallbackInfo &amp;info)
  {
      Napi::Function callback = info[0].As&lt;Napi::Function&gt;();
      jsCallback = &amp;jsCallback;
  
      // native library function for registering callback
      callback_add(OnReadyCallback);
  }
  
  void OnReadyCallback(Args *arg)
  {
     jsCallback-&gt;Call();
  }
  
  Napi::Object InitAll(Napi::Env env, Napi::Object exports)
  {
      exports.Set(String::New(env, "onReady"), Function::New(env, RegisterReadyEvent));
      return exports;
  }
  </code></pre>
  
  <p>The problem is the order of declaration. It seems <code>OnReadyCallback</code> has to be declared before RegisterReadyEvent function or it won't compile. But if I declare it before RegisterReadyEvent, jsCallback became an empty pointer and it still won't compile.</p>
  
  <p>Or is there a better and proper way to achieve the same thing?</p>