You've already forked tf2wikipricing
refactor: rewrite async promises as chain
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { defindex_key, storage_priceprefix } from "./config"
|
||||
import { priceUsingPricesTF } from "./pricing/pricestf"
|
||||
import { getStorageValue, setStorageValue } from "./storage"
|
||||
import { logDebug, log } from "./utils/log"
|
||||
import { logDebug } from "./utils/log"
|
||||
|
||||
/** Pricing data for a given TF2 item. */
|
||||
export class ItemPriceData {
|
||||
@@ -40,7 +40,6 @@ export async function fetchKeyPrice(token: string) {
|
||||
* @param ttl Time to cache results in milliseconds. 30 minutes by default.
|
||||
*/
|
||||
export async function fetchPrice(token: string, sku: string, update: Date = new Date(), ttl: number = 30 * 60 * 1000): Promise<ItemPriceData> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let data: ItemPriceData | null
|
||||
|
||||
const cached: ItemPriceData = await getStorageValue(storage_priceprefix + sku, null)
|
||||
@@ -51,7 +50,7 @@ export async function fetchPrice(token: string, sku: string, update: Date = new
|
||||
if (!data || data.sku != sku || 'update' in data && 'ttl' in data && Date.now() > (new Date(data.update).getTime() + data.ttl)) {
|
||||
logDebug(`Fetching price data for ${sku}`)
|
||||
if(!token) {
|
||||
reject(401)
|
||||
throw new Error('No token provided')
|
||||
}
|
||||
data = new ItemPriceData()
|
||||
data.sku = sku
|
||||
@@ -65,8 +64,7 @@ export async function fetchPrice(token: string, sku: string, update: Date = new
|
||||
data.metal = response.metal
|
||||
}
|
||||
} catch (error) {
|
||||
log(`Received ${error} error while pricing ${sku} using prices.tf`)
|
||||
reject(error)
|
||||
throw new Error(`Received ${error} error while pricing ${sku} using prices.tf`)
|
||||
}
|
||||
|
||||
if ('metal' in data && 'keys' in data) {
|
||||
@@ -75,6 +73,5 @@ export async function fetchPrice(token: string, sku: string, update: Date = new
|
||||
} else {
|
||||
logDebug(`Using cached price data for ${sku}`)
|
||||
}
|
||||
resolve(data)
|
||||
})
|
||||
return data
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user