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?
workboxBuild.injectManifestwill return a promisebuildSW, but it looks like they're commenting onworkboxBuild.injectManifest. The code usage also implies this since any return value frombuildSWis ignored.buildSW()without treating the return value, it wouldn't make a difference, but yes, it would make more sense if there was thereturnstatement.buildSWwould be above its implementation. It doesn't look likebuildSWwas intended to return anything, rather to callinjectManifestand do some stuff with its returned promise, and that's all.return, I put a link of the original example, here's another one: lifesaver.codes/answer/guidlines-for-using-workbox and another from CRA of Facebook (they created the example) github.com/facebook/create-react-app/issues/5673