diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 4ee9c5b583dc..b5985e4b7406 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1851,7 +1851,7 @@ describe('BrowserWindow module', function () { }) }) - it('resolves the returned promise with the result', function (done) { + it('resolves the returned promise with the result when a callback is specified', function (done) { ipcRenderer.send('executeJavaScript', code, true) ipcRenderer.once('executeJavaScript-promise-response', function (event, result) { assert.equal(result, expected) @@ -1859,6 +1859,14 @@ describe('BrowserWindow module', function () { }) }) + it('resolves the returned promise with the result when no callback is specified', function (done) { + ipcRenderer.send('executeJavaScript', code, false) + ipcRenderer.once('executeJavaScript-promise-response', function (event, result) { + assert.equal(result, expected) + done() + }) + }) + it('resolves the returned promise with the result if the code returns an asyncronous promise', function (done) { ipcRenderer.send('executeJavaScript', asyncCode, true) ipcRenderer.once('executeJavaScript-promise-response', function (event, result) { diff --git a/spec/static/main.js b/spec/static/main.js index cfc21249b592..1ac489ecae8b 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -193,16 +193,23 @@ app.on('ready', function () { }) ipcMain.on('executeJavaScript', function (event, code, hasCallback) { + let promise + if (hasCallback) { - window.webContents.executeJavaScript(code, (result) => { + promise = window.webContents.executeJavaScript(code, (result) => { window.webContents.send('executeJavaScript-response', result) - }).then((result) => { - window.webContents.send('executeJavaScript-promise-response', result) - }).catch((err) => { - window.webContents.send('executeJavaScript-promise-error', err) }) } else { - window.webContents.executeJavaScript(code) + promise = window.webContents.executeJavaScript(code) + } + + promise.then((result) => { + window.webContents.send('executeJavaScript-promise-response', result) + }).catch((error) => { + window.webContents.send('executeJavaScript-promise-error', error) + }) + + if (!hasCallback) { event.returnValue = 'success' } })