Media play() Promise Rejection Names
play() returns a promise whose rejection name explains the block: AbortError when pause lands first, NotAllowedError when autoplay policy wants a gesture.
Code
// Media elements: play() returns a promise, and the rejection name
// explains why playback did not start
const video = document.createElement('video');
video.src = 'sample.webm';
document.body.append(video);
const pending = video.play();
console.log('play() returned a promise:', pending instanceof Promise);
// pausing before playback begins rejects the pending play() with AbortError
video.pause();
pending
.then(() => console.log('playback started'))
.catch((err) => console.log('pending play rejected with:', err.name));
// NotAllowedError = autoplay blocked without a user gesture; muted
// playback skips that gesture requirement in most browsers
video.muted = true;
video.play().catch((err) => console.log('muted play rejected with:', err.name));How it works
Top-level bindings video, pending hold the demo state — nothing sits inside a function scope.
Watch out for
- This is browser-side code —
documentwill not resolve in Node.
Using it
Output lines are prefixed (play() returned a promise:, playback started, pending play rejected with:), making each step’s effect easy to spot.
Key statements
const video = document.createElement('video');
const pending = video.play();
document.body.append(video);
Interactive Tools for This Topic
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.CSP Header BuilderBuild a Content-Security-Policy header from per-directive sources, skipping empty ones and rejecting a blanket wildcard.
FAQ
- Does Media play() Promise Rejection Names run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.