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

+31 -44
View File
@@ -7,58 +7,45 @@ export type AccountListWithServer = (Account & {
server: 'com' | 'eu' | 'asia';
})[];
export default function listAccountsPanServer(search: string, limit = 9) {
return new Promise<AccountListWithServer>(async (resolve) => {
const trimmed = search.trim();
if (trimmed.length < 3 && trimmed.length > 100) resolve([]);
export default async function listAccountsPanServer(search: string, limit = 9) {
const trimmed = search.trim();
if (trimmed.length < 3 && trimmed.length > 100) return [];
const normalizedLimit = Math.round(limit / 3);
const normalizedLimit = Math.round(limit / 3);
const accountList: AccountListWithServer = [];
let done = 0;
function afterResponse() {
done++;
if (done === 3) {
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) => ({
return (
await Promise.all([
getWargamingResponse<AccountList>(
`https://api.wotblitz.com/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
).then(
(value) =>
value &&
value.map((account) => ({
...account,
server: 'com' as BlitzServer,
})),
);
afterResponse();
});
getWargamingResponse<AccountList>(
`https://api.wotblitz.eu/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
).then((value) => {
if (value)
accountList.push(
...value.map((account) => ({
),
getWargamingResponse<AccountList>(
`https://api.wotblitz.eu/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
).then(
(value) =>
value &&
value.map((account) => ({
...account,
server: 'eu' as BlitzServer,
})),
);
afterResponse();
});
getWargamingResponse<AccountList>(
`https://api.wotblitz.asia/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
).then((value) => {
if (value)
accountList.push(
...value.map((account) => ({
),
getWargamingResponse<AccountList>(
`https://api.wotblitz.asia/wotb/account/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
).then(
(value) =>
value &&
value.map((account) => ({
...account,
server: 'asia' as BlitzServer,
})),
);
afterResponse();
});
});
),
])
)
.filter(Boolean)
.flat();
}
+31 -44
View File
@@ -7,58 +7,45 @@ export type ClanListWithServer = (Clan & {
server: 'com' | 'eu' | 'asia';
})[];
export default function listClansPanServer(search: string, limit = 9) {
return new Promise<ClanListWithServer>(async (resolve) => {
const trimmed = search.trim();
if (trimmed.length < 3 && trimmed.length > 100) resolve([]);
export default async function listClansPanServer(search: string, limit = 9) {
const trimmed = search.trim();
if (trimmed.length < 3 && trimmed.length > 100) return [];
const normalizedLimit = Math.round(limit / 3);
const normalizedLimit = Math.round(limit / 3);
const accountList: ClanListWithServer = [];
let done = 0;
function afterResponse() {
done++;
if (done === 3) {
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) => ({
return (
await Promise.all([
getWargamingResponse<ClanList>(
`https://api.wotblitz.com/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
).then(
(value) =>
value &&
value.map((account) => ({
...account,
server: 'com' as BlitzServer,
})),
);
afterResponse();
});
getWargamingResponse<ClanList>(
`https://api.wotblitz.eu/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
).then((value) => {
if (value)
accountList.push(
...value.map((account) => ({
),
getWargamingResponse<ClanList>(
`https://api.wotblitz.eu/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
).then(
(value) =>
value &&
value.map((account) => ({
...account,
server: 'eu' as BlitzServer,
})),
);
afterResponse();
});
getWargamingResponse<ClanList>(
`https://api.wotblitz.asia/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
).then((value) => {
if (value)
accountList.push(
...value.map((account) => ({
),
getWargamingResponse<ClanList>(
`https://api.wotblitz.asia/wotb/clans/list/?application_id=${args['wargaming-application-id']}&search=${search}&limit=${normalizedLimit}`,
).then(
(value) =>
value &&
value.map((account) => ({
...account,
server: 'asia' as BlitzServer,
})),
);
afterResponse();
});
});
),
])
)
.filter(Boolean)
.flat();
}