For work, I made an example of how to cache a fetch
promise.
This is different than awaiting a promise and saving the result. Subsequent calls will use the same promise.
It outputs alternating logs:
-
Image object.
-
String "bad dog".
After the loop there is one more setTimeout
call. But there is no extra fetch
in the network traffic.
The magic is response.clone()
whereas directly calling response.json()
again would cause an error.