summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/respond-then-throw-worker.js
blob: adb48de69e72351ab0aca1df5be3757da0a93796 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var syncport = null;

self.addEventListener('message', function(e) {
  if ('port' in e.data) {
    if (syncport) {
      syncport(e.data.port);
    } else {
      syncport = e.data.port;
    }
  }
});

function sync() {
  return new Promise(function(resolve) {
      if (syncport) {
        resolve(syncport);
      } else {
        syncport = resolve;
      }
    }).then(function(port) {
      port.postMessage('SYNC');
      return new Promise(function(resolve) {
          port.onmessage = function(e) {
            if (e.data === 'ACK') {
              resolve();
            }
          }
        });
    });
}


self.addEventListener('fetch', function(event) {
    // In Firefox the result would depend on a race between fetch handling
    // and exception handling code. On the assumption that this might be a common
    // design error, we explicitly allow the exception to be handled first.
    event.respondWith(sync().then(() => new Response('intercepted')));

    throw("error");
  });