admin管理员组文章数量:1279120
i'm struggling with promises in a service worker while using async/await
syntax.
Following situation: I got a push notification and want to handle the click event. If i use the "old" syntax with then
and catch
i can iteratore over the list of clients and do something with it. If i use my prefered way with async/await
it wouldn't do anything.
self.addEventListener("notificationclick", event => {
// is working
event.waitUntil(self.clients.matchAll().then(clientList => {
console.log(clientList);
}));
// is not working
event.waitUntil(async () => {
const clientList = await self.clients.matchAll();
console.log(clientList);
});
});
i'm struggling with promises in a service worker while using async/await
syntax.
Following situation: I got a push notification and want to handle the click event. If i use the "old" syntax with then
and catch
i can iteratore over the list of clients and do something with it. If i use my prefered way with async/await
it wouldn't do anything.
self.addEventListener("notificationclick", event => {
// is working
event.waitUntil(self.clients.matchAll().then(clientList => {
console.log(clientList);
}));
// is not working
event.waitUntil(async () => {
const clientList = await self.clients.matchAll();
console.log(clientList);
});
});
Share
Improve this question
asked Nov 15, 2017 at 23:30
dkirchhofdkirchhof
3851 gold badge3 silver badges11 bronze badges
3
-
2
Looks like
waitUntil
takes a promise as it's arugment, not a function. Maybe if you invoke your async function immediately, it would work. eg:event.waitUntil( (async () => { ... })() )
– CRice Commented Nov 15, 2017 at 23:33 -
@CRice is correct, your passing a function here not a promise. You could do the IIF way, but I'd say a more obvious easy to understand way would be to create a function called
getClients
that's got all your async stuff, and then doevent.waitUntil(getClients());
– Keith Commented Nov 15, 2017 at 23:53 -
@CRice and @Keith Thanks both of you. You're totally right. I mixed some things up... I also tried to use an "external" async function, because i knew that it will return a promise. But actually i just passed its reference instead of calling it (
waitUntil(getClients)
vswaitUntil(getClients())
– dkirchhof Commented Nov 16, 2017 at 0:50
2 Answers
Reset to default 9Thanks to @Crice and @Keith,
waitUntil need a promise as argument instead of a function. So this is the working example in async/await style:
self.addEventListener("notificationclick", event =>
{
event.waitUntil(getClients());
});
async function getClients()
{
const clientList = await self.clients.matchAll();
console.log(clientList);
}
You can modify your original code to make an async IIFE as
// should be working now
event.waitUntil( (async () => {
const clientList = await self.clients.matchAll();
console.log(clientList);
})() );
The added ( ), will immediately invokes the async function hence the name async IIFE
版权声明:本文标题:javascript - service workers: async await in combination with waituntil is not working properly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741240576a2363895.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论