respond-then-throw-worker.js (999B)
1 var syncport = null; 2 3 self.addEventListener('message', function(e) { 4 if ('port' in e.data) { 5 if (syncport) { 6 syncport(e.data.port); 7 } else { 8 syncport = e.data.port; 9 } 10 } 11 }); 12 13 function sync() { 14 return new Promise(function(resolve) { 15 if (syncport) { 16 resolve(syncport); 17 } else { 18 syncport = resolve; 19 } 20 }).then(function(port) { 21 port.postMessage('SYNC'); 22 return new Promise(function(resolve) { 23 port.onmessage = function(e) { 24 if (e.data === 'ACK') { 25 resolve(); 26 } 27 } 28 }); 29 }); 30 } 31 32 33 self.addEventListener('fetch', function(event) { 34 // In Firefox the result would depend on a race between fetch handling 35 // and exception handling code. On the assumption that this might be a common 36 // design error, we explicitly allow the exception to be handled first. 37 event.respondWith(sync().then(() => new Response('intercepted'))); 38 39 throw("error"); 40 });