Skip to content

Commit c826628

Browse files
committed
Handle timeouts using abortcontroller
1 parent 98fb7aa commit c826628

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/cnames.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,15 @@ const generateCNAMEsFile = async (cnames, file) => {
152152
*/
153153
const testUrl = async url => {
154154
let resp;
155+
const controller = new AbortController();
156+
const timer = setTimeout(() => controller.abort(), 5000);
155157
try {
156-
resp = await fetch(url, { timeout: 5000 });
158+
resp = await fetch(url, { signal: controller.signal });
157159
} catch (err) {
160+
if (err.name === "AbortError") return "Failed due to time out after 5s"
158161
return `Failed during request with error '${err}'`
162+
} finally {
163+
clearTimeout(timer);
159164
}
160165
if (!resp.ok) {
161166
return `Failed with status code '${resp.status} ${resp.statusText}'`

0 commit comments

Comments
 (0)