Use Async functions and callbacks in background #389
-
Hi, I'm currently working on this extension that needs to call an async function and a couple async callbacks in the background service worker. The expected behavior is for it to run all functions like any other javascript environment would, however the current behavior has the background.js file not running at all, it doesn't throw an error either. Help on this would be much appreciated. BTW thanks for the awesome framework, it saved me a lot of time and configuration. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey, thanks for asking. Not sure what's going wrong without seeing your code, but here's an example extension that makes an network request in the background. Note that the background's main function cannot be async, because you need to register all extension listeners synchronously when the service worker starts up. So to call an async API, like wxt-async-background-example.zip async-example.mp4 |
Beta Was this translation helpful? Give feedback.
Hey, thanks for asking. Not sure what's going wrong without seeing your code, but here's an example extension that makes an network request in the background.
Note that the background's main function cannot be async, because you need to register all extension listeners synchronously when the service worker starts up. So to call an async API, like
fetch
, you have to use.then
or a callback to handle the result.wxt-async-background-example.zip
async-example.mp4