Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix explanation in runtime.onMessage doc #3903

Merged
merged 2 commits into from
May 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ <h3 id="Parameters">Parameters</h3>
<p>The <code><var>listener</var></code> function can return either a Boolean or a {{jsxref("Promise")}}.</p>

<div class="notecard note">
<p><strong>Important:</strong> Do not call <code>addListener()</code> using an <code>async</code> function:</p>
<p><strong>Important:</strong> If you pass an async function to <code>addListener()</code>, the listener will return a Promise for every message it receives, preventing other listeners from responding:</p>

<pre class="brush: js example-bad">// don't do this
browser.runtime.onMessage.addListener(
Expand All @@ -121,15 +121,14 @@ <h3 id="Parameters">Parameters</h3>
);
</pre>

<p>This will cause the listener to consume every message it receives, effectively blocking all other listeners from receiving and processing messages.</p>

<p>If you want to take an asynchronous approach, use a Promise instead, like this:</p>
<p>If you only want the listener to respond to messages of a certain type, you must define the listener as a non-<code>async</code> function, and return a Promise only for the messages the listener is meant to respond to — and otherwise return false or undefined:</p>

<pre class="brush: js example-good">browser.runtime.onMessage.addListener(
  (data, sender) =&gt; {
if (data.type === 'handle_me') {
  return Promise.resolve('done');
  }
return false;
}
);
</pre>
Expand Down