Electron ipcRenderer.sendSync() returning undefined

919 Views Asked by At

I'm writing an app with the electron-react-boilerplate framework.

I've been following a tutorial here: https://riptutorial.com/electron/example/19713/synchronous-ipc-communication

In my renderer process I have:

let a = window.electron.ipcRenderer.sendSync('LIST', []);
console.log(a);

and in my main process:

ipcMain.on('LIST', (event) => {
  event.returnValue = 'test';
});

However the value 'a' is undefined when it is logged. What am I doing wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

I realised what I did wrong.

In preload.ts I exposed the function sendSync like below:

sendSync(channel: Channels, args: unknown[]) {
  ipcRenderer.sendSync(channel, args);
},

...this didn't return anything, hence the undefined. I should have done:

sendSync(channel: Channels, args: unknown[]) {
  return ipcRenderer.sendSync(channel, args);
},

Now it returns as expected!