Skip to content

将 electron 的 ipcMain 与 ipcRenderer 间的通信转换为 promise 模式

Notifications You must be signed in to change notification settings

think2011/ipc2promise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ipc2promise

将 electron 的 ipcMain 与 ipcRenderer 间的通信转换为 promise 模式

安装

npm

$ npm i ipc2promise

使用

主线程(main process)

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)

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.

About

将 electron 的 ipcMain 与 ipcRenderer 间的通信转换为 promise 模式

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published