Adjust addrcheck timeouts (jest 27) (#353)

This commit is contained in:
Jaco
2021-05-31 15:16:45 +03:00
committed by GitHub
parent 3909583107
commit 95eee85d2e
5 changed files with 30 additions and 8 deletions
+6 -1
View File
@@ -31,7 +31,12 @@ async function loopSome (site: string, matcher: () => Promise<string[] | null>):
// console.error(error);
}
await new Promise<boolean>((resolve) => setTimeout(() => resolve(true), Math.floor((Math.random() * 750) + 1000)));
await new Promise<boolean>((resolve) =>
setTimeout(
() => resolve(true),
Math.floor((Math.random() * 750) + 1000)
)
);
}
return [site, found];
+19 -4
View File
@@ -6,10 +6,25 @@ import { fetch } from '@polkadot/x-fetch';
// a fetch with a 2s timeout
export async function fetchWithTimeout (url: string, timeout = 2000): Promise<Response> {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(url, { signal: controller.signal });
let isAborted = false;
const id = setTimeout((): void => {
console.log(`Timeout on ${url}`);
clearTimeout(id);
isAborted = true;
controller.abort();
}, timeout);
return response;
try {
const response = await fetch(url, { signal: controller.signal });
clearTimeout(id);
return response;
} catch (error) {
if (!isAborted) {
clearTimeout(id);
}
throw error;
}
}