<p dir="auto">Consider the following snippet run in the main process where one wants to listen for <a href="http://electronjs.org/docs/api/web-contents#event-will-prevent-unload" rel="nofollow"><code class="notranslate">"will-prevent-default"</code></a> events and after some <em>asynchronous</em> operation (<code class="notranslate">do_something</code>, a call which returns a <code class="notranslate">Promise</code>) initiated by the event listener, depending on the result (<code class="notranslate">result</code>) of the operation, call <code class="notranslate">preventDefault</code> on the event to allow page to unload:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="/// Assume `win` refers to your typical application window (a `BrowserWindow` instance)
win.webContents.on(&quot;will-prevent-default&quot;, ev =&gt; {
    do_something().then(result =&gt; {
        if(result == some_value) {
            ev.preventDefault(); /// Does NOT work (page doesn't unload) when called from a callback, i.e. when not called immediately (synchronously) in the event handler.
        }
    });
    /// `ev.preventDefault` would have the documented effect if one placed the call here, e.g. somewhere it would be executed as part of the event handler itself, not a callback.
});"><pre class="notranslate"><span class="pl-c">/// Assume `win` refers to your typical application window (a `BrowserWindow` instance)</span>
<span class="pl-s1">win</span><span class="pl-kos">.</span><span class="pl-c1">webContents</span><span class="pl-kos">.</span><span class="pl-en">on</span><span class="pl-kos">(</span><span class="pl-s">"will-prevent-default"</span><span class="pl-kos">,</span> <span class="pl-s1">ev</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-en">do_something</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">then</span><span class="pl-kos">(</span><span class="pl-s1">result</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
        <span class="pl-k">if</span><span class="pl-kos">(</span><span class="pl-s1">result</span> <span class="pl-c1">==</span> <span class="pl-s1">some_value</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
            <span class="pl-s1">ev</span><span class="pl-kos">.</span><span class="pl-en">preventDefault</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">/// Does NOT work (page doesn't unload) when called from a callback, i.e. when not called immediately (synchronously) in the event handler.</span>
        <span class="pl-kos">}</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-c">/// `ev.preventDefault` would have the documented effect if one placed the call here, e.g. somewhere it would be executed as part of the event handler itself, not a callback.</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">One would expect <code class="notranslate">preventDefault</code> to work as documented regardless of when it is called?</p>
<p dir="auto">Is this a known issue, and is this simply a bug?</p>
<p dir="auto">Electron 6.0.10 win32 x64<br>
Microsoft Windows 10 64-bit Enterprise build 1709</p>
<ul dir="auto">
<li>Output of <code class="notranslate">node_modules/.bin/electron --version</code>: <strong>v4.0.1</strong></li>
<li>Operating System (Platform and Version):
<ul dir="auto">
<li>OS: <strong>Linux, Xubuntu (Ubuntu)</strong>.</li>
<li>Version: <strong>18.04.1 LTS (Bionic Beaver)</strong>.</li>
<li>Kernel version: <strong>4.15.0-43-generic</strong></li>
</ul>
</li>
<li>Output of <code class="notranslate">node_modules/.bin/electron --version</code> on last known working Electron version (if applicable): <strong>v5.0.1</strong></li>
</ul>
<p dir="auto"><strong>Procedure</strong><br>
I put <code class="notranslate">win.setMenu(null)</code> directly under the browser window creation/declaration:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="// Create the browser window.
win = new BrowserWindow({ width: 800, height: 600 })

// Hide the menu bar
win.setMenu(null)

..."><pre class="notranslate"><code class="notranslate">// Create the browser window.
win = new BrowserWindow({ width: 800, height: 600 })

// Hide the menu bar
win.setMenu(null)

...
</code></pre></div>
<p dir="auto"><strong>Expected Behavior</strong><br>
I expected the menu bar to be hidden at window creation.</p>
<p dir="auto"><strong>Actual behavior</strong><br>
The menu was still showing.</p>