首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从异步函数-电子返回值的Node.js

从异步函数-电子返回值的Node.js
EN

Stack Overflow用户
提问于 2022-06-20 14:55:20
回答 1查看 69关注 0票数 0

我正在创建一个项目,在这个项目中,我需要接受用户的输入--通过函数传递它并将新的值返回给用户--看起来很简单。我是异步函数的新手,我已经阅读了我所能读到的所有东西,如果我缺少一个更基本的问题,我也无法解决。我将展示基本代码,然后是我想要实现的。我相信问题是,我返回的是函数的状态,而不是值,但无法解决。

“基本法典”:

代码语言:javascript
复制
ipcMain.on('gpt3', (event, args) => {
    async function gpt3(args) {
        generateResponse('james', 'hello world'); // Takes a user's name & input and recieves a response from a python file.
        event.reply('textRecieve', 'hello world'); // Sends 'Hello World' to the user (ipcRenderer 'textRecieve')
    }
    
    gpt3(args);

})

async function generateResponse(name, text) {
    let testshell = new PythonShell('./python/text_echo.py', { mode: 'text', args: [name, text]});
    let content = "";
    try {
      testshell.on('message', function (message) {
        console.log(message); // prints the output from the python file 'Python File: james Text: hello world'
        return message; // attempting to return the 'Message' from the python file
      });
    } catch (error) {
      console.log("You've f*cked it somewhere my friend");
      console.log(error);
    } 
}

Python脚本:

代码语言:javascript
复制
import sys

name = sys.argv[1]
text = sys.argv[2]

print(f'Python File: {name} Text: {text}')
sys.stdout.flush()

返回:(如预期)

代码语言:javascript
复制
> Executing task: npm run start <


> electron-quick-start@1.0.0 start
> electron .

Python File: james Text: hello world

我想做的是:

代码语言:javascript
复制
ipcMain.on('gpt3', (event, args) => {
    async function gpt3(args) {
        message = generateResponse('james', 'hello world'); // Takes a user's name & input and recieves a response from a python file, retunring the message to the 'message' variable.
        console.log(message);
        event.reply('textRecieve', 'message would send here'); // Sends the 'Message' to the user (ipcRenderer 'textRecieve')
    }

    gpt3(args);

  })


async function generateResponse(name, text) {
  let testshell = new PythonShell('./python/text_echo.py', { mode: 'text', args: [name, text]});
  let content = ""
  try {
    testshell.on('message', function (message) {
      console.log(message); // prints the output from the python file 'Python File: james Text: hello world'
      return message; // attempting to return the 'Message' from the python file
    });
  } catch (error) {
    console.log("You've f*cked it somewhere my friend")
    console.log(error)
  } 
  return content; // content needs to be message instead due to async nature it returns empty string
}

返回:

代码语言:javascript
复制
> Executing task: npm run start <


> electron-quick-start@1.0.0 start
> electron .

Promise { '' }
Python File: james Text: hello world

TLDR;我想将通过'generateResponse()‘生成的’消息‘传递给'event.reply()’。相反,我正在接受我认为是承诺的地位。任何帮助都将不胜感激。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-20 18:25:11

好吧,这里有一些问题.但主要是当‘异步’时,node.js‘无法’传递变量。( node.js )对我来说是新的,我不能撒谎说我很困惑。希望下面的链接到一个很好的解决方案/方法和我的工作代码将能够帮助某人:

https://stackoverflow.com/a/23667087/10246221

代码:ipcMain -嵌套在app.whenReady()中。

代码语言:javascript
复制
ipcMain.on('gpt3', (event, input) => {
    gpt3Async(event, input, function(result) { 
      event.reply('textRecieve', result);
      console.log('gpt3Async: '+ result);
      })

  })

代码:泛型‘嵌套’ Function --自由浮动在'main.js‘或'index.js’之间。

代码语言:javascript
复制
function gpt3Async(event, input, callback) {
    console.log('input: ' + input)
    let testshell = new PythonShell('./python/text_echo.py', { mode: 'text', args: ['elliott' ,input]});
    testshell.on('message', function (message) {
      callback(message);
    });
}

代码:Python脚本'text_echo.py' --在我的例子中,它位于'python‘子目录中。

代码语言:javascript
复制
import sys

name = sys.argv[1]
text = sys.argv[2]

print(f'Python File: {name} Text: {text}')
#sys.stdout.flush()
sys.stdout.flush()

对于任何在您需要输入和输出python脚本的项目上工作的人,这将帮助您解决问题。还请确保打开以下内容:

代码语言:javascript
复制
webPreferences: {
      //preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
      sandbox: false,
    }, 

但是!,请注意这将对您的代码产生的安全影响,更多的信息可以在这里获得:https://stackoverflow.com/a/57507392 & https://electronjs.org/docs/tutorial/security#3-enable-context-isolation-for-remote-content &如果这是一个重要的项目,请阅读一些内容。

好吧,一个解释者,或者至少是一些让我作为初学者而震惊的东西.。我最终理解它的方式是通过示例链接:

https://stackoverflow.com/a/23667087/10246221

出于某种原因,它并没有和我一起点击,函数可以嵌套在这样的函数中,所有这些都在一行中。对于习惯于JS或node.js的人来说,这似乎是很基本的,但对我来说,这是第一次的项目,如果还在使用python代码的话,可能还有其他人。希望这能帮上忙!

代码语言:javascript
复制
ipcMain.on('gpt3', (event, input) => { gpt3Async(event, input, function(result) { event.reply('textRecieve', result); console.log('gpt3Async: '+ result);})})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72689008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档