Skip to content
This repository was archived by the owner on Mar 23, 2018. It is now read-only.

Commit 6da8316

Browse files
committed
fix(rule): support timeout error
1 parent 7cb9cdd commit 6da8316

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/fetch-proofdict.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
// MIT © 2018 azu
22
"use strict";
33
const fetch = require("fetch-ponyfill")().fetch;
4+
5+
function delayPromise(ms) {
6+
return new Promise(function(resolve) {
7+
setTimeout(resolve, ms);
8+
});
9+
}
10+
11+
function timeoutPromise(promise, ms) {
12+
const timeout = delayPromise(ms).then(function() {
13+
const error = new Error('Operation timed out after ' + ms + ' ms');
14+
error.name = "TimeoutError";
15+
throw error;
16+
});
17+
return Promise.race([promise, timeout]);
18+
}
19+
420
export function fetchProofdict({ URL }) {
5-
return fetch(URL).then(res => res.json());
21+
return timeoutPromise(fetch(URL), 5000).then(res => {
22+
if (!res.ok) {
23+
throw Error(res.statusText);
24+
}
25+
return res.json();
26+
}).catch(error => {
27+
if (error.name === "TimeoutError") {
28+
return;
29+
}
30+
return Promise.reject(error);
31+
});
632
}

0 commit comments

Comments
 (0)