0

What do I need to do to adjust the way this method returns to only return the value and not the Promise?:

function getCacheValue(toMatch){
     return caches.open("v1.1::Custom").then(cache=>{ cache.match(toMatch).then(result=>result).then(resultValue=>resultValue);});
}

Essentially what I would like it to return is the Response from case.match(). I'm still pretty new to asynchronous Promises, Everything I've tried ends up returning a Promise rather than the result of the Promise. I've also tried wrapping the caches.open call in a new Promise((res, rej){caches.open.....}) and calling res(resultValue) and also tried Promise.resolve(...) but I still can't seem to just get resultValue to return.

I'm pretty sure the issue here is the synchronous function is trying to get the result of an asynchronous call but I am fairly certain that should still be possible to accomplish.

Also, I would prefer the getCacheValue function to remain synchronous as I will end up synchronously sending different values into it if the result is null from the first call.

jay
  • 434
  • 1
  • 5
  • 25
  • "*I'm pretty sure the issue here is the synchronous function is trying to get the result of an asynchronous call*" - yes. You're trying to immediately access a value that doesn't exist until later. "*I am fairly certain that should still be possible to accomplish.*" - no. Only to the extent that time travel is possible. – Bergi Feb 01 '22 at 22:24
  • So you're telling me there's a chance. :-) – jay Feb 01 '22 at 22:29
  • My end goal here is a service worker that will first check to see if there is a matching cached result. If there is, it returns it, then there are 2 additional fallback pages for it to check so I was wanting to getCacheValue("page1") then getCacheValue("page2") then getCacheValue("page3") if the preceding page was not available. – jay Feb 01 '22 at 22:33
  • No, there's no chance - I meant specifically information travelling *back* in time. Don't even attempt it. Just embrace the asynchrony and do the sequential fallback checking inside the promise chain callback or with `await`. – Bergi Feb 01 '22 at 22:39

0 Answers0