Notification Permission Request Flow
Read Notification.permission without prompting, then call requestPermission inside a user gesture and branch on the resolved decision before showing a note.
Code
// Notification.permission exposes the current state without any prompt
const state = Notification.permission;
console.log('current permission:', state);
// requestPermission must run inside a user gesture and resolves
// with the final decision
async function permissionFlow() {
if (state === 'granted') return 'already granted';
if (state === 'denied') return 'blocked: user must clear site settings';
const decision = await Notification.requestPermission();
console.log('requestPermission resolved with:', decision);
if (decision === 'granted') {
const note = new Notification('Deploy finished', { body: 'build 42 is live' });
note.addEventListener('close', () => console.log('notification closed by user'));
return 'granted: notification shown';
}
return 'decision: ' + decision;
}
permissionFlow().then((message) => console.log(message));How it works
Everything happens inside the function permissionFlow.
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.
Points of care
- The guard
state === 'granted'cuts off everything after it by throwing an earlyreturnwhen its condition holds.
Using it
Entry signature: async function permissionFlow()
Output lines are prefixed (current permission:, requestPermission resolved with:, notification closed by user), making each step’s effect easy to spot.
Key statements
if (state === 'denied') return 'blocked: user must clear site settings';
const decision = await Notification.requestPermission();
if (state === 'granted') return 'already granted';
Interactive Tools for This Topic
Unix Permissions CalculatorConvert between octal and symbolic Unix file permissions and explain read/write/execute plus setuid, setgid, and sticky bits.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.
FAQ
- What has to be installed first?
- Nothing — the runtime itself provides everything the code touches. The file defines `permissionFlow` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.