我正在尝试对节点ffi进行异步调用,该节点返回一个int作为函数返回值,并修改传递给该函数的param。
var myLib = ffi.Library( 'mylib.dll', {
'JSCall' : ['int', ['int', 'string', strPtr, 'int']],
}); //strPtr is ref.refType('string');
我想使用 for 循环在异步模式下调用此库函数。这是我想要运行的代码;
//Callback function to use in async calls
var cbJSCall = function(err, data) {
if (err){
log.error('error in callback ' +err.message );
return console.log(err);
}
console.log('ffi's int response ' + data );
console.log('ffi's modified strPtr =' + ref.readCString(res, 0) '\n');
};
var asJSonCall = function(handle, req, buf){
myLib.JSCall.async(handle, req, res, res.length, cbJSCall);
}
//
var jsq = [] //string array that is passed to ffi as param req
//loop through the array and make async calls to myLib.JSCalls
for (var i=0;i<jsq.length;i++) {
var test = function(j) {
asJSonCall(handle, jsq[i], buf);
}();
}
代码运行,但输出不是我所期望的。我想我还没有完全理解闭包??任何帮助将不胜感激