Must the initial state of the Promise accepted by `ExtendableEvent.waitUntil()` be `"pending"`?

Viewed 79

On MDN, I saw the service worker selective caching sample quoted from GoogleChrome, then I adapted the parameters of event.waitUntil() in the 'activate' event handler in the file service-worker.js, and one of the variants throws an error:

Uncaught (in promise) DOMException: Failed to execute 'waitUntil' on 'ExtendableEvent': The event handler is already finished and no extend lifetime promises are outstanding.

For the sake of brevity, the conditional judgment and modification operations in the 'activate' event handler are omitted in the adapted version, then the core logic is:

self.addEventListener('activate', event =>
  event.waitUntil(
    /* #0 */ '...'
  )
)

The following are four adaptation variants:

self.addEventListener('activate', async event =>
  event.waitUntil(
    /* #1 */ Promise.all(await caches.keys())
  )
)
self.addEventListener('activate', event =>
  event.waitUntil(
    /* #2 */ Promise.resolve().then(async () => Promise.all(await caches.keys()))
  )
)
self.addEventListener('activate', event =>
  event.waitUntil(
    /* #3 */ async function () { return Promise.all(await caches.keys()) }()
  )
)
self.addEventListener('activate', event =>
  event.waitUntil(
    /* #4 */ caches.keys().then(cacheNames => Promise.all(cacheNames))
  )
)

Why does #1 throw DOMException?

Nevertheless, #2~4 are normal. And I can only observe that the initial value of [[PromiseStatus]] at #1 is "resolved".

Thank you very much!

0 Answers
Related