You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js Addon开发报错求助:Nan::AsyncQueue运行失败及语法错误

Hey there! Let's tackle your Node.js Addon issues one by one, starting with the obvious syntax error first since that's blocking your execution right away.

1. Fix the SyntaxError: Unexpected token )

Looking at your index.js code, the issue is a misplaced closing parenthesis in your callback function call. You've added an extra ) right after the function parameter list, which breaks JavaScript's syntax rules.

Corrected index.js code:

var m = require('./');
var vec1 = new m.Vector(20, 10, 0);
// Fixed callback syntax
vec1.add_number(20, function(err, res) {
  if(err) console.error(err);
  else console.log('Final Result=%d', res);
});

The mistake was putting ) after function(err,res) instead of wrapping the entire function body inside the parentheses of add_number(). This should resolve the syntax error immediately.

2. Troubleshooting Nan::AsyncQueue failure

Since your testworker.cc code is incomplete, I'll walk through the most common causes of this issue and how to fix them:

  • Check AsyncWorker implementation:
    Ensure your TestWorker class properly inherits from Nan::AsyncWorker, and initializes the callback correctly in its constructor. For example:

    class TestWorker : public Nan::AsyncWorker {
    public:
      TestWorker(Nan::Callback* callback, int addNum, YourVectorType& vec)
        : Nan::AsyncWorker(callback), addNum(addNum), vec(vec) {}
      ~TestWorker() {}
    
      // Background execution logic (runs on worker thread)
      void Execute() override {
        // Perform your vector addition here
        vec.add_number(addNum);
        // If an error occurs, set it with:
        // SetErrorMessage("Failed to add number to vector");
      }
    
      // Callback logic (runs on main JS thread)
      void HandleOKCallback() override {
        Nan::HandleScope scope;
        v8::Local<v8::Value> argv[] = {
          Nan::Null(), // No error, pass null
          // Pass your result back to JS (adjust type as needed)
          Nan::New<v8::Number>(vec.get_some_value())
        };
        callback->Call(2, argv);
      }
    
    private:
      int addNum;
      YourVectorType& vec; // Replace with your actual vector class type
    };
    
  • Verify Nan::AsyncQueueWorker call:
    In your Addon method that exposes add_number to JS, make sure you're correctly extracting the callback and creating/queuing the worker:

    NAN_METHOD(AddNumberToVector) {
      // Validate arguments
      if (!info[0]->IsNumber() || !info[1]->IsFunction()) {
        Nan::ThrowError("Expected number and callback function");
        return;
      }
    
      int addNum = Nan::To<int>(info[0]).FromJust();
      // Get your Vector instance from the JS object
      YourVectorType* vec = Nan::ObjectWrap::Unwrap<YourVectorType>(info.This());
      Nan::Callback* callback = new Nan::Callback(info[1].As<v8::Function>());
    
      // Queue the worker (this is where Nan::AsyncQueue happens under the hood)
      Nan::AsyncQueueWorker(new TestWorker(callback, addNum, *vec));
    }
    
  • Check for thread safety:
    Ensure any operations on your vector in the Execute() method are thread-safe—since this runs on a separate worker thread, you can't access V8 objects here, only raw C++ data.

If you're still hitting the Nan::AsyncQueue failure after checking these points, share the full testworker.cc code, and I can help pinpoint the exact issue.

内容的提问来源于stack exchange,提问作者rim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:59:09