Last active
November 21, 2018 18:26
-
-
Save rBurgett/f568fa703dabc40396a8676ac5f11553 to your computer and use it in GitHub Desktop.
Handling ipcMain events as Promises sequentially
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Renderer | |
(async function() { | |
ipcRenderer.removeAllListeners('something'); | |
ipcRenderer.removeAllListeners('somethingElse'); | |
// Set Promises to be resolved when the data comes in | |
const promises = [ | |
new Promise(resolve => { | |
ipcRenderer.once('something', (e, res) => { | |
resolve(res); | |
}); | |
}), | |
new Promise(resolve => { | |
ipcRenderer.once('somethingElse', (e, res) => { | |
resolve(res); | |
}); | |
}) | |
]; | |
ipcMain.send('event'); | |
// Loop over the promises and handle each one sequentially | |
for(const p of promises) { | |
const res = await p; | |
console.log(res); | |
}; | |
)(); | |
// Main | |
ipcMain.on('event', async function(e) { | |
const res = await doSomething(); | |
e.sender.send('something', res); | |
const res1 = await doSomethingElse(); | |
e.sentder.send('somethingElse', res2); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment