Skip to content

Instantly share code, notes, and snippets.

@rBurgett
Last active November 21, 2018 18:26
Show Gist options
  • Save rBurgett/f568fa703dabc40396a8676ac5f11553 to your computer and use it in GitHub Desktop.
Save rBurgett/f568fa703dabc40396a8676ac5f11553 to your computer and use it in GitHub Desktop.
Handling ipcMain events as Promises sequentially
// 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