Correct handle Promise returned from ipc


#1

Hello,

I’m new to this forum and new to electron, I’m trying to create a simple application with this logic:
from render thread I’m calling function of other modules using ipc and waiting the result.

What I do is send an ipc like this

function test(){
    var p = ipcRenderer.sendSync('mainChannel', 
     {   'class':'MyModule', 
         'method':'myPromise',
         'param':[]
      });

   console.log(p)
   p.then(function(){ console.log('done') });
}

which is passed to the method ‘myPromise’ of the module ‘MyModule’ with no params.
This is the method:

function myPromise(){
     return new Promise(function(resolve, reject) {
          setTimeout(() => resolve(4), 2000);
     });
}

so the method return a promise which is resolved after 2 seconds.
In function test() I have this results:

//=> "[object Promise]"
// "p.then is not a function" error

I red this post

and related link to github push.

which are aboute the same problem but they are old (2015) and seems that this problem should be solved but its still here or I don’t know how to make it works.

Thanks in advance.


#2

Replace electron's ipcMain.on() ipcRender.send()
to npm ipc-promise's ipcPromise.on(), ipcPromise.send()
work for me~ hope that helps u