Title: Dynamic Cache name in service worker
Tags: javascript;service-worker
Question: how create dynamic cache name in service worker?
I want to check API server to know the last version of my app. it will be cache name of my app.
I used fetch but it always return ```undefined```,

```staticCacheName = (() =&gt; {
    fetch('https://unitedspace.co.id/check_version.php')
        .then(response =&gt; response.json())
        .then(myJson =&gt; myJson.app_name + '-' + myJson.version + '.' + myJson.build_number);
})();
```

this is my service worker code :

```self.addEventListener('activate', function(e) {
    e.waitUntil(caches.keys().then(function(cacheNames) {
        return Promise.all(
        cacheNames.map(function(cacheName) {
            if (cacheName != 'blog-{{ site.github.build_revision }}') {
            return caches.delete(cacheName);
            }
        })
        );
    }));
});




self.addEventListener('fetch', function(e) {
    e.respondWith(caches.match(e.request).then(function(response) {
        return response || fetch(e.request);
    }));
});
```



Here is another answer: You do not have any return in your first snippet. is it normal ?

Comment for this answer: it return promise resolve, I think fetch only return promise only in first `then`, but it always return promise. I think I need to make all script in the bottom wait until my cache name success. but I don't know to do that
Comment for this answer: Where is used staticCacheName ?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:02/24/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><service-worker>', 'Title': 'Dynamic Cache name in service worker', 'ViewCount': 195, 'id': 60372110, 'user_AboutMe': None, 'user_AccountId': 5688321.0, 'user_DisplayName': 'Dedi Ananto', 'user_Reputation': 135.0, 'user_Views': 83.0, 'user_id': 4497114.0}}