JavaScript how to use event.waitUntil() to wait for an async function call - Stack Overflow

When learning about JavaScript it remends using asyncawait as it's much more readable than .then,

When learning about JavaScript it remends using async/await as it's much more readable than .then, and I agree. Unfortunately when it es to PWA service workers, async seems to be forgotten.

When trying to create an install function for a service worker with async style:

const cacheVersion = "v1";
const cacheAssets = [
    "/",
    "/index.html",
    "/static/img/logo.svg",
];

async function install(version, assets) {
    const cache = await caches.open(version);
    console.log("Doing stuff....");
}

self.addEventListener("install", (event) => {
    console.log("Service Worker INSTALL START");
    event.waitUntil(install, cacheVersion, cacheAssets); // <--- Does not call install()
    console.log("Service Worker INSTALL COMPLETE");
});

The install function is never run. If I change this line:

event.waitUntil(install, cacheVersion, cacheAssets);

to

install(cacheVersion, cacheAssets);

Then the line is run, but not waited for, causing problems.

How does one use event.waitUntil to call an async function and wait for it?

To me event.waitUntil is like await, so why have this weird special function that blocks the main loop, when it seems like nothing else in JS does? Just seems so strange to me.

When learning about JavaScript it remends using async/await as it's much more readable than .then, and I agree. Unfortunately when it es to PWA service workers, async seems to be forgotten.

When trying to create an install function for a service worker with async style:

const cacheVersion = "v1";
const cacheAssets = [
    "/",
    "/index.html",
    "/static/img/logo.svg",
];

async function install(version, assets) {
    const cache = await caches.open(version);
    console.log("Doing stuff....");
}

self.addEventListener("install", (event) => {
    console.log("Service Worker INSTALL START");
    event.waitUntil(install, cacheVersion, cacheAssets); // <--- Does not call install()
    console.log("Service Worker INSTALL COMPLETE");
});

The install function is never run. If I change this line:

event.waitUntil(install, cacheVersion, cacheAssets);

to

install(cacheVersion, cacheAssets);

Then the line is run, but not waited for, causing problems.

How does one use event.waitUntil to call an async function and wait for it?

To me event.waitUntil is like await, so why have this weird special function that blocks the main loop, when it seems like nothing else in JS does? Just seems so strange to me.

Share Improve this question asked Jan 1, 2021 at 13:35 run_the_racerun_the_race 2,4483 gold badges55 silver badges85 bronze badges 1
  • 1 Side note: I haven't played with service workers nearly enough, but I'm 99.9% sure your final console.log will say the worker is installed prematurely. I suggest putting that call at the end of install, rather than after the waitUntil call. waitUntil just tells the event subsystem that the work is ongoing. I very much doubt it blocks the thread. (But again, I haven't played with service workers enough yet.) – T.J. Crowder Commented Jan 1, 2021 at 13:42
Add a ment  | 

1 Answer 1

Reset to default 8

install isn't called because you don't call it. waitUntil accepts a promise, not a function. An async function isn't a promise, it's a function that returns a promise.

Call your function and pass the promise it returns into waitUntil:

event.waitUntil(install(cacheVersion, cacheAssets));
// −−−−−−−−−−−−−−−−−−−−^−−−−−−−−−−−−−−−−−−−−−−−−−^

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744733634a4590610.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信