Javascript 服务工人及;承诺时间问题

Javascript 服务工人及;承诺时间问题,javascript,Javascript,如果缓存为空(或过期),我试图扩展google docs解决方案,从服务器获取数据,但在承诺的时间上遇到了问题 我的代码如下: self.addEventListener("fetch", function(event) { if (event.request.url.indexOf("targetPage") !== -1) { const cacheName = 'myCacheName'; event.respondWith(

如果缓存为空(或过期),我试图扩展google docs解决方案,从服务器获取数据,但在承诺的时间上遇到了问题

我的代码如下:

self.addEventListener("fetch", function(event) {
    if (event.request.url.indexOf("targetPage") !== -1) {
        const cacheName = 'myCacheName';
        event.respondWith(
            caches.open(cacheName).then(function(cache) {
                return cache.match(event.request).then(function(response) {
                    if(response !== undefined){
                        for (var pair of response.headers.entries()) {
                            if(pair[0] === 'date'){
                                var tokenExpiryDate = Date.parse(pair[1]);
                                var now = Date.parse(Date());
                                if(tokenExpiryDate < now){
                                    return response;
                                }
                                else{
                                    fetch(event.request).then(function(serverResponse) {
                                        cache.put(event.request, serverResponse.clone());
                                        return serverResponse;
                                    })
                                }
                            }
                        }
                    }
                    else {
                        fetch(event.request).then(function(serverResponse) {
                            cache.put(event.request, serverResponse.clone()); return serverResponse;
                        })
                    };
                });
            })
        );
    }
});
self.addEventListener("fetch", function(event) {
    if (event.request.url.indexOf("targetPage") !== -1) {
        const cacheName = event.request.headers.get("myCache");
        event.respondWith(
            caches.open(cacheName).then(function(cache) {
                return cache.match(event.request).then(function(response) {
                    return (
                        response ||
                        fetch(event.request).then(function(response) {
                            cache.put(event.request, response.clone());
                            return response;
                        })
                    );
                });
            })
        );
    }
});

因此,通过尽可能接近原始解决方案,我能够使其正常工作:

self.addEventListener("fetch", function(event) {
    if (event.request.url.indexOf("targetpage") !== -1) {
        const cacheName = "myCache";
        event.respondWith(
            caches.open(cacheName).then(function(cache) {
                return cache.match(event.request).then(function(response) {
                    return (
                        GetCachedResponse(response) ||
                        fetch(event.request).then(function(response) {
                            cache.put(event.request, response.clone());
                            return response;
                        })
                    );
                });
            })
        );
    }
});

function GetCachedResponse(response) {
    if (response !== undefined) {
        for (var pair of response.headers.entries()) {
            if (pair[0] === "token-expires") {
                var tokenExpiryDate = Date.parse(pair[1]);
                var now = Date.parse(Date());
                if (tokenExpiryDate > now) {
                    return response;
                }
            }
        }
    }
}

因此,通过尽可能接近原始解决方案,我能够使其正常工作:

self.addEventListener("fetch", function(event) {
    if (event.request.url.indexOf("targetpage") !== -1) {
        const cacheName = "myCache";
        event.respondWith(
            caches.open(cacheName).then(function(cache) {
                return cache.match(event.request).then(function(response) {
                    return (
                        GetCachedResponse(response) ||
                        fetch(event.request).then(function(response) {
                            cache.put(event.request, response.clone());
                            return response;
                        })
                    );
                });
            })
        );
    }
});

function GetCachedResponse(response) {
    if (response !== undefined) {
        for (var pair of response.headers.entries()) {
            if (pair[0] === "token-expires") {
                var tokenExpiryDate = Date.parse(pair[1]);
                var now = Date.parse(Date());
                if (tokenExpiryDate > now) {
                    return response;
                }
            }
        }
    }
}

有点背景就好了。你的例子是从哪里来的?什么是
self
?@DavidKnipe问题用谷歌的原始代码更新您在
fetch(…)
调用前缺少
return
关键字。别忘了你总是需要
返回
一些东西!你还没有回答我的问题。事实上,关于你的问题是关于什么的,你没有给出太多的线索。Stackoverflow适用于各种各样的问题。这可能是香草javascript,也可能是谷歌提供的某个库,也可能是某个打开谷歌页面托管文档的家伙创建的库。我没有办法知道。这些标签也没有多大用处——我们从标签中所知道的只是它与javascript有关。当你发帖的时候,请记住这一点。一些上下文会很好。你的例子是从哪里来的?什么是
self
?@DavidKnipe问题用谷歌的原始代码更新您在
fetch(…)
调用前缺少
return
关键字。别忘了你总是需要
返回
一些东西!你还没有回答我的问题。事实上,关于你的问题是关于什么的,你没有给出太多的线索。Stackoverflow适用于各种各样的问题。这可能是香草javascript,也可能是谷歌提供的某个库,也可能是某个打开谷歌页面托管文档的家伙创建的库。我没有办法知道。这些标签也没有多大用处——我们从标签中所知道的只是它与javascript有关。发帖时请记住这一点。