https://github.com/think2011/ipc2promise
将 electron 的 ipcMain 与 ipcRenderer 间的通信转换为 promise 模式
https://github.com/think2011/ipc2promise
electron ipc ipcmain ipcrenderer promise
Last synced: 12 months ago
JSON representation
将 electron 的 ipcMain 与 ipcRenderer 间的通信转换为 promise 模式
- Host: GitHub
- URL: https://github.com/think2011/ipc2promise
- Owner: think2011
- Created: 2017-12-08T05:37:20.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-02T03:53:00.000Z (over 8 years ago)
- Last Synced: 2025-06-01T05:16:07.037Z (about 1 year ago)
- Topics: electron, ipc, ipcmain, ipcrenderer, promise
- Language: JavaScript
- Size: 342 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ipc2promise
将 electron 的 ipcMain 与 ipcRenderer 间的通信转换为 promise 模式

## 安装
### npm
```sh
$ npm i ipc2promise
```
## 使用
### 主线程(main process)
```js
const ipc2promise = require('ipc2promise')
ipc2promise.on('download', async (event, data, resolve ,reject) => {
try {
resolve(await download(data.url))
} catch (err) {
reject(new Error(err))
}
})
```
### 渲染进程(renderer process)
```js
const ipc2promise = require('ipc2promise')
ipc2promise.send('download', {url: 'https://xxxxxxxx.com/pkg.zip'})
.then((filePath) => {
console.log(filePath)
})
.catch((err) => {
console.error('Oops..')
})
```
## API
### on(event, fn)
- `event`
- `String` - 事件名
- `fn`
- `Function(event, data, resolve, reject)` event: event, data: 获得的数据, resolve: 通过, reject: 拒绝
### send(event, data)
- `event`
- `String` - 事件名
- `data`
- `*` - 要发送的数据
## License
The MIT license.