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.

JavascriptSecurity Boundaries

Code

javascriptsnippet
20 lines
// 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 — document will 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

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.