refactor: rewrite async promises as chain

This commit is contained in:
xenticore
2025-04-30 18:31:33 -04:00
parent 36550df749
commit 5e5846abde
4 changed files with 67 additions and 98 deletions

View File

@@ -227,21 +227,17 @@ async function inject() {
// Check item schema for Australium variant of current defindex
if(itemSchema[itemIndex].hasAustraliumVariant) {
promises.push(new Promise(async (resolve) => {
logDebug(`Fetching price for Australium ${itemName}`)
let data: ItemPriceData | null
try {
data = await fetchPrice(token, `${itemIndex};11;australium`, currentTime);
updateTime = new Date(data.update)
} catch {
log(`Australium ${itemName} is unpriced or unavailable, skipping...`)
}
promises.push(fetchPrice(token, `${itemIndex};11;australium`, currentTime).then(data => {
updateTime = new Date(data.update)
log(`Saving price for Australium ${itemName}`)
const priceRow = createPriceRow($T("Australium"), data, keyPrice, exchangeRates, locale, "https://wiki.teamfortress.com/wiki/Australium_weapons")
priceRows.push({order: 99, row: priceRow, category: PriceRowCategory.None})
resolve()
return
})
.catch((error) => {
logError(error)
log(`Australium ${itemName} is unpriced or unavailable, skipping...`)
}))
}
@@ -259,37 +255,29 @@ async function inject() {
festiveHeadingRow.style.display = 'none';
festiveHeadingRow.appendChild(festiveHeading);
promises.push(new Promise(async (resolve) => {
logDebug(`Fetching price for Festive ${itemName}`)
let data: ItemPriceData | null
try {
data = await fetchPrice(token, `${itemSchema[itemIndex].festiveVariant};6`, currentTime);
updateTime = new Date(data.update)
} catch {
log(`Festive ${itemName} is unpriced or unavailable, skipping...`)
}
promises.push(fetchPrice(token, `${itemSchema[itemIndex].festiveVariant};6`, currentTime).then(data => {
updateTime = new Date(data.update)
log(`updateTime price for Festive ${itemName}`)
const priceRow = createPriceRow($T("Unique"), data, keyPrice, exchangeRates, locale)
priceRows.push({order: -1, row: priceRow, category: PriceRowCategory.Festive})
resolve()
return
})
.catch((error) => {
logError(error)
log(`Festive ${itemName} is unpriced or unavailable, skipping...`)
}))
promises.push(new Promise(async (resolve) => {
logDebug(`Fetching price for Strange Festive ${itemName}`)
let data: ItemPriceData | null
try {
data = await fetchPrice(token, `${itemSchema[itemIndex].festiveVariant};11`, currentTime);
updateTime = new Date(data.update)
} catch {
log(`Strange Festive ${itemName} is unpriced or unavailable, skipping...`)
}
promises.push(fetchPrice(token, `${itemSchema[itemIndex].festiveVariant};11`, currentTime).then(data => {
updateTime = new Date(data.update)
log(`Saving price for Strange Festive ${itemName}`)
const priceRow = createPriceRow($T("Strange"), data, keyPrice, exchangeRates, locale)
priceRows.push({order: 11, row: priceRow, category: PriceRowCategory.Festive})
resolve()
return
})
.catch((error) => {
logError(error)
log(`Strange Festive ${itemName} is unpriced or unavailable, skipping...`)
}))
}
@@ -310,30 +298,23 @@ async function inject() {
killstreakKitHeadingRow.style.display = 'none';
killstreakKitHeadingRow.appendChild(heading);
[1,2,3].map((tier) => {
promises.push(new Promise(async (resolve) => {
logDebug(`Fetching price for ${itemName} Killstreak Kit Tier ${tier}`)
let data: ItemPriceData | null
try {
let kitIndex: number
switch (tier) {
default:
case 1: kitIndex = 6527; break;
case 2: kitIndex = 6523; break;
case 3: kitIndex = 6526; break;
}
data = await fetchPrice(token, `${kitIndex};6;uncraftable;kt-${tier};td-${itemIndex}`, currentTime);
updateTime = new Date(data.update)
} catch {
log(`${itemName} Killstreak Kit Tier ${tier} is unpriced or unavailable, skipping...`)
resolve()
return
}
let kitIndex: number
switch (tier) {
default:
case 1: kitIndex = 6527; break;
case 2: kitIndex = 6523; break;
case 3: kitIndex = 6526; break;
}
promises.push(fetchPrice(token, `${kitIndex};6;uncraftable;kt-${tier};td-${itemIndex}`, currentTime).then(data => {
updateTime = new Date(data.update)
logDebug(`Saving price for ${itemName} Killstreak Kit Tier ${tier}`)
const priceRow = createPriceRow($T(`kt-${tier}`), data, keyPrice, exchangeRates, locale, "https://wiki.teamfortress.com/wiki/Killstreak_Kit")
priceRows.push({order: tier, row: priceRow, category: PriceRowCategory.KillstreakKit})
resolve()
return
})
.catch((error) => {
logError(`Failed to fetch price for ${itemName} Killstreak Kit Tier ${tier}`, error)
}))
})
}
@@ -366,23 +347,17 @@ async function inject() {
const itemName = itemSchema[variantIndex].name
// FIXME: variantName should match wiki display name
const variantName = itemName.includes('Mk.II') ? itemName.split(' ')[0] + ' Mk.II' : itemName.split(' ')[0]
promises.push(new Promise(async (resolve) => {
logDebug(`Fetching price for ${itemName}`)
let data: ItemPriceData | null
try {
data = await fetchPrice(token, `${variantIndex};11`, currentTime);
updateTime = new Date(data.update)
} catch {
log(`${itemName} is unpriced or unavailable, skipping...`)
}
promises.push(fetchPrice(token, `${variantIndex};11`, currentTime).then(data => {
logDebug(`Saving price for ${itemName}`)
updateTime = new Date(data.update)
const priceRow = createPriceRow($T(variantName), data, keyPrice, exchangeRates, locale, "https://wiki.teamfortress.com/wiki/Botkiller_weapons")
// FIXME: order should be by release
// Silver Mk.I, Gold Mk.II, Rust, Blood, Carbonado, Diamond, Silver Mk.II, Gold Mk.II
priceRows.push({order: botkillerOrder.indexOf(variantName), row: priceRow, category: PriceRowCategory.Botkiller})
resolve()
return
})
.catch((error) => {
logError(error)
log(`Strange Festive ${itemName} is unpriced or unavailable, skipping...`)
}))
})
}