feat: use Promise.all

This commit is contained in:
TresAbhi committed 2023-05-20 01:22:20 +00:00
1 parent c45b80c40e
commit c6f462f0d7
5 files changed
+69 -88

No files matched your search

+1
View File
@@ -17,6 +17,7 @@
"node-fetch": "^3.3.1" "node-fetch": "^3.3.1"
}, },
"devDependencies": { "devDependencies": {
"@total-typescript/ts-reset": "^0.4.2",
"@types/lodash.startcase": "^4.4.7", "@types/lodash.startcase": "^4.4.7",
"@types/markdown-escape": "^1.1.0", "@types/markdown-escape": "^1.1.0",
"@types/node": "^20.2.1", "@types/node": "^20.2.1",
+31 -44
View File
@@ -7,58 +7,45 @@ export type AccountListWithServer = (Account & {
server: 'com' | 'eu' | 'asia'; server: 'com' | 'eu' | 'asia';
})[]; })[];
export default function listAccountsPanServer(search: string, limit = 9) { export default async function listAccountsPanServer(search: string, limit = 9) {
return new Promise<AccountListWithServer>(async (resolve) => { const trimmed = search.trim();
const trimmed = search.trim(); if (trimmed.length < 3 && trimmed.length > 100) return [];
if (trimmed.length < 3 && trimmed.length > 100) resolve([]); const normalizedLimit = Math.round(limit / 3);
const normalizedLimit = Math.round(limit / 3); return (
const accountList: AccountListWithServer = []; await Promise.all([
let done = 0; getWargamingResponse<AccountList>(
`https://api.wotblitz.com/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
function afterResponse() { ).then(
done++; (value) =>
value &&
if (done === 3) { value.map((account) => ({
resolve(accountList);
}
}
getWargamingResponse<AccountList>(
`https://api.wotblitz.com/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
).then((value) => {
if (value)
accountList.push(
...value.map((account) => ({
...account, ...account,
server: 'com' as BlitzServer, server: 'com' as BlitzServer,
})), })),
); ),
afterResponse(); getWargamingResponse<AccountList>(
}); `https://api.wotblitz.eu/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
getWargamingResponse<AccountList>( ).then(
`https://api.wotblitz.eu/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, (value) =>
).then((value) => { value &&
if (value) value.map((account) => ({
accountList.push(
...value.map((account) => ({
...account, ...account,
server: 'eu' as BlitzServer, server: 'eu' as BlitzServer,
})), })),
); ),
afterResponse(); getWargamingResponse<AccountList>(
}); `https://api.wotblitz.asia/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
getWargamingResponse<AccountList>( ).then(
`https://api.wotblitz.asia/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, (value) =>
).then((value) => { value &&
if (value) value.map((account) => ({
accountList.push(
...value.map((account) => ({
...account, ...account,
server: 'asia' as BlitzServer, server: 'asia' as BlitzServer,
})), })),
); ),
afterResponse(); ])
}); )
}); .filter(Boolean)
.flat();
} }
+31 -44
View File
@@ -7,58 +7,45 @@ export type ClanListWithServer = (Clan & {
server: 'com' | 'eu' | 'asia'; server: 'com' | 'eu' | 'asia';
})[]; })[];
export default function listClansPanServer(search: string, limit = 9) { export default async function listClansPanServer(search: string, limit = 9) {
return new Promise<ClanListWithServer>(async (resolve) => { const trimmed = search.trim();
const trimmed = search.trim(); if (trimmed.length < 3 && trimmed.length > 100) return [];
if (trimmed.length < 3 && trimmed.length > 100) resolve([]); const normalizedLimit = Math.round(limit / 3);
const normalizedLimit = Math.round(limit / 3); return (
const accountList: ClanListWithServer = []; await Promise.all([
let done = 0; getWargamingResponse<ClanList>(
`https://api.wotblitz.com/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
function afterResponse() { ).then(
done++; (value) =>
value &&
if (done === 3) { value.map((account) => ({
resolve(accountList);
}
}
getWargamingResponse<ClanList>(
`https://api.wotblitz.com/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
).then((value) => {
if (value)
accountList.push(
...value.map((account) => ({
...account, ...account,
server: 'com' as BlitzServer, server: 'com' as BlitzServer,
})), })),
); ),
afterResponse(); getWargamingResponse<ClanList>(
}); `https://api.wotblitz.eu/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
getWargamingResponse<ClanList>( ).then(
`https://api.wotblitz.eu/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, (value) =>
).then((value) => { value &&
if (value) value.map((account) => ({
accountList.push(
...value.map((account) => ({
...account, ...account,
server: 'eu' as BlitzServer, server: 'eu' as BlitzServer,
})), })),
); ),
afterResponse(); getWargamingResponse<ClanList>(
}); `https://api.wotblitz.asia/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
getWargamingResponse<ClanList>( ).then(
`https://api.wotblitz.asia/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`, (value) =>
).then((value) => { value &&
if (value) value.map((account) => ({
accountList.push(
...value.map((account) => ({
...account, ...account,
server: 'asia' as BlitzServer, server: 'asia' as BlitzServer,
})), })),
); ),
afterResponse(); ])
}); )
}); .filter(Boolean)
.flat();
} }
+1
View File
@@ -0,0 +1 @@
import '@total-typescript/ts-reset';
+5
View File
@@ -179,6 +179,11 @@
resolved "https://registry.npmjs.org/@tokenizer/token/-/token-0.3.0.tgz" resolved "https://registry.npmjs.org/@tokenizer/token/-/token-0.3.0.tgz"
integrity sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A== integrity sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A==
"@total-typescript/ts-reset@^0.4.2":
version "0.4.2"
resolved "https://registry.yarnpkg.com/@total-typescript/ts-reset/-/ts-reset-0.4.2.tgz#c564c173ba09973968e1046c93965b7a257878a4"
integrity sha512-vqd7ZUDSrXFVT1n8b2kc3LnklncDQFPvR58yUS1kEP23/nHPAO9l1lMjUfnPrXYYk4Hj54rrLKMW5ipwk7k09A==
"@types/lodash.startcase@^4.4.7": "@types/lodash.startcase@^4.4.7":
version "4.4.7" version "4.4.7"
resolved "https://registry.yarnpkg.com/@types/lodash.startcase/-/lodash.startcase-4.4.7.tgz#69857f17be0aa5ce74da4fa386e32e22c012258a" resolved "https://registry.yarnpkg.com/@types/lodash.startcase/-/lodash.startcase-4.4.7.tgz#69857f17be0aa5ce74da4fa386e32e22c012258a"