如何从外部脚本JavaScript回调返回

How to return from callback from external script JavaScript

本文关键字:回调 返回 JavaScript 脚本 从外部      更新时间:2023-09-26

我已经用nodeJS配置了python-shell。我试图通过调用回调函数从pythonShell返回。回调函数能够成功地从pythonShell控制台记录结果,但是,我无法返回它。我相信这是因为PythonShell.run不支持返回。

// Python Invocation
exports.find = function (searchterm) {
  var PythonShell = require('python-shell');
  function callback (err, results) {
    console.log(results); //Exhibit A (logs successfully)
  }
  // PythonShell does not support return
  PythonShell.run('findAnswer.py', callback);
  //How do I return Exhibit A?
};

任何关于这方面的建议都会很感激

谢谢

你可以这样修改你的函数

exports.find = function (searchterm, callback) {
  var PythonShell = require('python-shell');
  // PythonShell does not support return
  PythonShell.run('findAnswer.py', callback);
};
var finds = require('modulename').find;
finds('search term', function(err, results){
 //do anything with results here
});