将 electron 的 ipcMain 与 ipcRenderer 间的通信转换为 promise 模式
$ npm i ipc2promise
const ipc2promise = require('ipc2promise')
ipc2promise.on('download', async (event, data, resolve ,reject) => {
try {
resolve(await download(data.url))
} catch (err) {
reject(new Error(err))
}
})
const ipc2promise = require('ipc2promise')
ipc2promise.send('download', {url: 'https://xxxxxxxx.com/pkg.zip'})
.then((filePath) => {
console.log(filePath)
})
.catch((err) => {
console.error('Oops..')
})
event
String
- 事件名
fn
Function(event, data, resolve, reject)
event: event, data: 获得的数据, resolve: 通过, reject: 拒绝
event
String
- 事件名
data
*
- 要发送的数据
The MIT license.