使用子进程,我执行 Python 脚本会执行一些吐回数据的操作。我使用 Node 承诺等到我得到 Python 数据。
我面临的问题是匿名函数有一个回调,回调接受两个参数,其中一个是 python 数据。下面的代码解释。我如何调用承诺,等到它解决然后调用回调。
节点承诺
var spawn = require("child_process").spawn;
function sensorData()
{
return new Promise(function(resolve, reject)
{
var pythonProcess = spawn ("python",[pythonV1.py"]);
pythonProcess.stdout.on("data", function(data)
{
resolve(data);
});
});
}
匿名函数
...
onReadRequest : function(offest, callback)
{
#============DOES NOT WORK=========================
sensorData()
.then(function(data)
{
callback(this.RESULT_SUCCESS, data);
})
#===================================================
#call promise, wait and then call callback passing the python data
callback(this.RESULT_SUCCESS, new Buffer(#python data)
}
...
非常感谢