I try to make my node module methods asynchronous. I created an AsyncWorker Class but stumbling on a few things :/
My Nan::AsyncWorker looks as follows...
class PackerWorker : public Nan::AsyncWorker {
public:
PackerWorker(Nan::Callback *callback, v8::Handle<v8::Array> messageFields)
: AsyncWorker(callback), messageFields(messageFields) {}
~PackerWorker() {}
// Executed inside the worker-thread.
// It is not safe to access V8, or V8 data structures
// here, so everything we need for input and output
// should go on `this`.
void Execute () {
//pack_iso8583(messageFields);
}
// Executed when the async work is complete
// this function will be run inside the main event loop
// so it is safe to use V8 again
void HandleOKCallback () {
//Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = { // currently just dummy values need to return v8::Local<v8::Array> result here!
Nan::Null()
, Nan::New<v8::Number>(1.75)
};
callback->Call(2, argv);
}
private:
v8::Handle<v8::Array> messageFields;
//v8::Local<v8::Array> result;
};
--
void Execute () {
result = pack_iso8583(messageFields);
}
When I implement this method the call crash without additional informations. Further reading point out that it is not safe to access V8, or V8 data structures. So I think this is the reason because the sync method works.
- Do I need to convert all variables to
Nan
datatypes? If so what is the equivalent inNan::
tov8::Local<v8::Array>
andv8::Handle<v8::Array>
and how convert them and vice versa? - How to get usefull exception information when the
C++
code fails to execute?
The complete source code is available here: https://github.com/s-a/iso-8583/blob/30ac35796ba5014ad5668ac2919fddcb3b082580/functions.cc#L144.
Just clone the repo and npm install
and npm test
Maybe someone like to create a PR :), anyway I am very pleased about any helping hints! I just hoped it would be more trivial to make existing methods asynchronous ^^