New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
event.waitUntil vs return promise #1148
Comments
Sorry, missed those. Seems like discussion went to hooks there. Is there any reason to not keep events + use return value? |
I don't understand how a single promise can replace both of these. |
In both cases, it waits for the given promise to resolve. For waitUntil, the value the promise resolves to is irrelevant/unused. |
But a fetch event has both |
fwiw the general problem here is event handler return values are already used for preventing default. Making them do something different events seems a bit weird. Also, if you don't want to handle a fetch, you just avoid calling |
Sure, but it seems like it should be fairly simple to not confuse the two. For example, if the promise return value was spec'ed just for ExtendableEvent, it would just make it work for SW and newer API's whilst leaving the older stuff as is.
I imagine very few people actually use event return values to prevent default vs On the other hand, people are pretty used to the event loop running till completion and will find the default behaviour very surprising. There will be lots of confusion and debugging when event handlers silently skip and finish tasks. It will require constantly changing between paradigms. In fact, now I think about it,
This could match to not returning a promise, or a promise that resolves to undefined? |
The browser will want to do this if you're using too much background CPU. It can use extendable events to track which events have granted the service worker additional run time, and that run time can be revoked. Additionally, |
The browser can intervene and terminate based on clear thresholds (time, CPU, storage, etc) in both cases, the Also note that you took a browser-perspective: I mentioned a developer will never not want to
The suggestion is that you can simply schedule asynchronous tasks that run to completion or if they trip a quota (e.g. CPU usage), and you can use the return value to signal any eventual extra info (success/failure/response/etc). It might be useful to create a gist to demonstrate if there's a certain task/pattern that you think this wouldn't work for? |
Yep, and it could know which events it terminated early, which wouldn't be the case if
It's pretty common to not
addEventListener('install', event => {
event.waitUntil(doAsyncStuff());
}); In the above example, if the promise returned by addEventListener('fetch', event => {
event.respondWith(
fetch(whatever).then(response => {
event.waitUntil(addThisToTheCache(response));
return response;
})
);
}); In the above example, the developer declares they want to handle this request by calling |
(Sorry for the delayed response)
Are you saying the browser can't know the list of outstanding tasks it terminated early without the waitUntil assist? I imagine the browser should have that knowledge à la
For completeness: "a developer will never not want to waitUntil if they have scheduled something asynchronously". That's always a bug. They might want to respond early, but not skip waiting.
addEventListener('install', event => {
return doAsyncStuff();
}); Similarly, does not install if promise rejects.
addEventListener('fetch', async ({ respond }) => {
const response = await fetch(whatever)
respond(response)
addThisToTheCache(response)
}) Similarly, the user can respond early and then cache stuff later. |
Currently, with fetch, you have to call |
To be clear, I think separating
— https://jakearchibald.com/2017/await-vs-return-vs-return-await/ You basically need to amend this article with another hazard where
This is the case without The issue is that To summarise: Keep |
Currently SW event listeners require using
event.respondWith
andevent.waitUntil
.It would be nice if we could return a promise instead of these.
The argument against given here was that because "it lets you keep the SW alive forever". However, you could pass the promise
sleep(Infinity)
toevent.waitUntil
too, so there doesn't seem to be anything special about theevent.*
syntax over using anasync
function +return
value. That is, the browser can kill the SW in both cases if it's taking too long.The text was updated successfully, but these errors were encountered: