【问题标题】:Accessing a return object/value from Electron ipcRenderer.invoke()从 Electron ipcRenderer.invoke() 访问返回对象/值
【发布时间】:2020-12-07 19:34:11
【问题描述】:

全局声明“response_”,调用简单函数

    function getResponse() {
        ipcRenderer.invoke('responseChan').then((result) => {
            response_ = result
            console.log('getResponse invocation returned: ',response_)
        })
        return response_
    }

表现如预期,在 promise 被解析时异步提供预期的函数返回值/对象。

但是 .invoke 方法是如何用于等待此类“getResponse”函数中的响应值/对象的呢?当编码如下时,我很难访问它:

    async function getResponse() {
        response_ = await ipcRenderer.invoke('responseChan')
        return response_
    }

正如对这个异步函数的预期,渲染器控制台记录:

    getResponse returned:  Promise {<pending>}__proto__: Promise
    [[PromiseStatus]]: "resolved"
    [[PromiseValue]]: "On",

但是需要什么方法来访问已解析的 PromiseValue(在本例中为“On”)?

主要代码是:

ipcMain.handle('responseChan', (respAwaited) => {
   response = visca_decode()
   console.log('visca_decode response: ', response)   
   mainWindow.webContents.send('camRespChan', response)
   let resp = (response === 'Stby') ? 'Camera 1 on Standby' : 'Camera 1 Powered and Selected'
   mainWindow.webContents.send('displayChan', resp)   
   //mainWindow.webContents.send('displayChan', 'Command acknowledged')
   return response
})

【问题讨论】:

  • 我不太理解这个问题。您是说 response_ 设置为 promsie 而不是实际结果?您需要向我们展示您的 ipcMain.handle("responseChan") 代码是什么样的
  • 是的; getResponse 使用 '.then' 方法异步提供结果“On”(当 promise 被解决时)。我理解 async getResponse .. await 在承诺解决之前不应返回,但结果对于调用该函数的编码没有意义。
  • 两个测试用例的主处理程序代码都没有改变:
  • 在对 ipcRenderer.invoke 的调用中没有“等待”,预计 getResponse 最初只返回一个承诺 - 但包括“等待”显然不会改变行为,暂停执行直到承诺解决.也许我误解了'await'的功能。
  • 对 async .. await 用法的误解导致了这个问题的特定措辞,也许应该写成“如何测试一个承诺是否已经解决?”可以应用全局信号量,当函数执行在“等待”之后恢复时设置 - 并进行测试以确定承诺解决方案。是否有更直接的方法来确定 Promise 的状态,以避免潜在的代码竞争错误?

标签: javascript electron


【解决方案1】:

对 async .. await 用法的误解导致了问题的特定措辞,可能应该是“如何测试承诺是否已解决”。

信号量的使用是显而易见的,当函数执行在“等待”之后恢复时设置 - 并测试以确定解决方案,除非有更直接的方法来确定承诺状态,以避免潜在的代码竞争错误。

通过将相关代码片段移动到 getResponse 中来避免导致原始问题的竞争条件,如下所示。这种变化可能会导致不同的竞争条件,除非存在对承诺状态的直接测试,否则可以通过使用信号量来解决。

  async function getResponse() {
  response_ = 'Response_ pending'
  console.log('response_ initialisation: ', response_)
  response_ = await ipcRenderer.invoke('responseChan')  //After calling .invoke,
              //getResponse returns with response_ set to the unresolved promise
  console.log('"Awaited" response_: ', response_)     //Succeeding code executes
  //return response_                //asynchronously, on resolution of the promise
  switch (response_) {              //This switch statement, which failed on
    case 'Stby':                    //unresolved promise when coded in line with
      args = [0, 'CAM_Power', 'On'] //call to getResponse, works here, since it
      response = executeCommand(args) //executes only when the promise is resolved!
      console.log('Switch stmt recognized: Camera 1 powered and selected')
      break;

...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    相关资源
    最近更新 更多