我正在尝试实现以下内容。在渲染器进程中,我有一个异步函数foo()
,它接受一个可选参数bar
作为回调函数引用。
在success
从主进程(此处未显示)接收并从主进程获取返回对象后,它对该对象执行一些操作,然后调用我作为参数传递的回调函数。
现在,为什么需要它是我有两个函数func1
,func2
第一个函数foo
与bar
提供的一起调用。第二个没有。
我该如何实施?
import { ipcRenderer } from "electron";
// in renderer
async function foo(/*optional function reference*/ bar) {
ipcRenderer.send('message', {
//options
});
ipcRenderer.on('success', (event, obj) => {
//process obj
bar(); //callback function passed as argument
})
}
async function func1() {
foo(bar)
}
async function func2() {
foo() //no function passed
}
function bar() {
console.log("hello world")
}