permissions.query Gates Feature Access

Check navigator.permissions.query({ name }) before using a gated API, branch on granted/denied/prompt, and listen for runtime state changes.

JavascriptSecurity Boundaries

Code

javascriptsnippet
15 lines
// permissions.query reports the current grant state without prompting
const status = await navigator.permissions.query({ name: 'geolocation' });
console.log('geolocation:', status.state);

// gate the feature path on the answer instead of calling and failing
if (status.state === 'granted') {
  console.log('safe to watch position immediately');
} else if (status.state === 'denied') {
  console.log('show settings help instead of a broken button');
}

// the state can change at runtime: 'change' fires on grant or revoke
status.addEventListener('change', () => {
  console.log('geolocation changed:', status.state);
});

What the snippet does

Every asynchronous step is awaited, so rejections surface where the call happens.

Top-level bindings status hold the demo state — nothing sits inside a function scope.

What to watch for

  • The environment needs no preparation beyond having the interpreter or compiler.
  • A browser is the runtime here: navigator are not available in Node.

Applying it

The run emits 4 labeled sections — geolocation:, safe to watch position immediately, show settings help instead of a broken button, geolocation changed: — one per step.

Statements that matter

status.addEventListener('change', () => {
  console.log('geolocation changed:', status.state);
});
const status = await navigator.permissions.query({ name: 'geolocation' });

Interactive Tools for This Topic

FAQ

Is the dependency story complicated?
Not at all: zero dependencies, so there is nothing to version or audit. Its state lives in module-level bindings: `status`.