Modernizr async tests

126 Views Asked by At

I have a test I'm doing to see if audio can be played under Chrome's media engagement score for the current window. It seems like a neat thing to wrap up as a Modernizr test. The test is something like:

Modernizr.addTest('audioallowed', () => {
   return new Audio().play().catch((e) => my logic returning true or false);
});

But this is asynchronouse / returns a promise. How do I put that in a Modernizr test? I can't find anything in the docs. I assume I would use it differently:

Modernizr.audioallowed.then(() = {
   //But I don't see anything like this in the docs...
});
1

There are 1 best solutions below

0
Miloš Đakonović On

Unlike addTest, Modernizr does not expose addAsyncTest method. And, addTest method, on which you have to relay, expect synchronous return. No synchronous return === test is interpreted as failed.

Regarding your specific task: maybe to reverse logic upside down?

new Audio().play().catch((e) => my logic returning true or false);

to become

new Audio().play().catch(function(e){
  
  var result;

  if(/*condition*/){
    result = true;
  } // else result = false;

  Modernizr.addTest('audioallowed', result);
});