Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

cache blogposts for 24 hours react native

const cacheIntervaInHours = 24
const cacheExpiryTime = new Date()
cacheExpiryTime.setHours(cacheExpiryTime.getHours() + cacheIntervalInHours)
const lastRequest = await AsyncStorage.getItem("lastRequest")

if (lastRequest == null || lastRequest > cacheExpiryTime) {
        fetch(`${apiUrl}/blogPosts/recent`)
        .then(async (response) => {
            return await response.json()
        })
        .then(async (json) => {
             if (!json || json.length == 0) {
                 throw new Error()
             }
            AsyncStorage.setItem("lastRequest", new Date());
            return await AsyncStorage.setItem('blogPosts', JSON.stringify(json))
        })
        .catch(error => {
            console.error(error)
        })
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular select option default value ngfor 
Javascript :: change class on resize window jquery 
Javascript :: map and get last child in js 
Javascript ::  
Javascript :: typeorm sqlite Using Entity Manager 
Javascript :: typeorm caching queries 
::  
Javascript :: javascript returns odd 
Javascript :: req.parms en react js 
Javascript :: after storing array array state is empty 
:: stdi nodejs 
:: how to format date dd/mm/yyyy in javascript 
Javascript ::  
Javascript ::  
Javascript :: golang json time 
:: Subtracting Numbers in Array 
::  
Javascript :: reset default style javascript 
Javascript ::  
Javascript ::  
::  
::  
::  
::  
Javascript ::  
:: js video controls false 
Javascript :: Cannot GET /public/staffRegister in node.js 
:: webpack no chunks 
:: rxjs fromevent mouseover 
Javascript :: return a specific value filter javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =