async function unitIndex(language) {
	
	try {
    	
        let expiredDate = new Date();
	        expiredDate.setHours(expiredDate.getHours() - eventCooldown);
		
		/** Get units from cache */
		let units = await cache.get('swapi', 'units', {language:language, updated:{ $gte:expiredDate.getTime() }});

		/** Check if existance and expiration */
		if( !units || !units[0] ) { 
		
			/** If not found or expired, fetch new from API and save to cache */
			units = {
			    units:await swgoh.fetchData({
                    "collection": "unitsList",
                    "language": language,
                    "enums":true,
                    "match": {
                    	"rarity": 7
                    },
                    "project": {
                        "baseId": 1,
                        "nameKey": 1,
                        "descKey": 1,
                        "forceAlignment": 1,
                        "combatType": 1,
                        "categoryIdList": 1,
                        "skillReferenceList": 1,
                        "crewList": 1 
                    }
                }),
			    language:language,
			    updated:(new Date()).getTime()
			}
			
			if( !units.units ) { throw new Error('Error fetching units'); } 
			
			units = await cache.put('swapi', 'units', {language:language}, units);

		} else {		
		    units = units[0];
		}

		return units;
		
	} catch(e) { 
		throw e; 
	}    		

}
/**
 *  Fetch localized units index from cache, or sync if necessary
 * 
 */
Fetch localized units index from cache, or sync if necessary
Fetch localized units index from cache or sync if necessary
async function unitIndex(language) {
	try {
        let expiredDate = new Date();
	        expiredDate.setHours(expiredDate.getHours() - eventCooldown);
		let units = await cache.get('swapi', 'units', {language:language, updated:{ $gte:expiredDate.getTime() }});
		if( !units || !units[0] ) { 
			units = {
			    units:await swgoh.fetchData({
                    "collection": "unitsList",
                    "language": language,
                    "enums":true,
                    "match": {
                    	"rarity": 7
                    },
                    "project": {
                        "baseId": 1,
                        "nameKey": 1,
                        "descKey": 1,
                        "forceAlignment": 1,
                        "combatType": 1,
                        "categoryIdList": 1,
                        "skillReferenceList": 1,
                        "crewList": 1 
                    }
                }),
			    language:language,
			    updated:(new Date()).getTime()
			}
			if( !units.units ) { throw new Error('Error fetching units'); } 
			units = await cache.put('swapi', 'units', {language:language}, units);
		} else {		
		    units = units[0];
		}
		return units;
	} catch(e) { 
		throw e; 
	}    		
}
async function unitIndex ( language ) { try { let expiredDate = new Date ( ) ; expiredDate . setHours ( expiredDate . getHours ( ) - eventCooldown ) ; let units = await cache . get ( 'swapi' , 'units' , { language : language , updated : { $gte : expiredDate . getTime ( ) } } ) ; if ( ! units || ! units [ 0 ] ) { units = { units : await swgoh . fetchData ( { "collection" : "unitsList" , "language" : language , "enums" : true , "match" : { "rarity" : 7 } , "project" : { "baseId" : 1 , "nameKey" : 1 , "descKey" : 1 , "forceAlignment" : 1 , "combatType" : 1 , "categoryIdList" : 1 , "skillReferenceList" : 1 , "crewList" : 1 } } ) , language : language , updated : ( new Date ( ) ) . getTime ( ) } if ( ! units . units ) { throw new Error ( 'Error fetching units' ) ; } units = await cache . put ( 'swapi' , 'units' , { language : language } , units ) ; } else { units = units [ 0 ] ; } return units ; } catch ( e ) { throw e ; } }
Fetch localized units index from cache, or sync if necessary
Fetch localized units index from cache or sync if necessary
/** Get units from cache */ /** Check if existance and expiration */ /** If not found or expired, fetch new from API and save to cache */