I've just seen this code:
const buildSW = () => {
// This will return a Promise <- that comment was present on the code
workboxBuild
.injectManifest({
swSrc: 'src/sw.js'
// some code
})
.then(({ count, size, warnings }) => {
// some code
})
.catch(console.error);
};
buildSW();
The code seems to be working, has been in production like 1 year, but I get confused with the comment of This will return a Promise
because I don't see that there is actually a return
statement, and the whole code is inside { }.
Shouldn't be?:
return workboxBuild
.injectManifest({ ...etc
The code is part from this guides:
https://developers.google.com/web/tools/workbox/guides/generate-service-worker/workbox-build
where the return
promise is present there. So how or why is working without return in the code showed above?
I get confused with the comment because I don't see that there is actually a
return
statement, like it is present in the guide. Shouldn't it have one?
Yes, there should be a return
statement. Sure, the function already catches errors and logs them, but it's still a good idea to always return a promise from asynchronous functions.
So how or why is working without return in the code showed above?
It's working only because the buildSW();
call you're doing doesn't try to use the promise that should be returned.