UNPKG

666 BPlain TextView Raw
1
2import 'isomorphic-unfetch';
3
4export function allSettled(promises: Promise<any>[], catcher = () => null) {
5 return Promise.all(promises.map(promise => promise.catch(catcher)));
6}
7
8export async function fetchWithTimeout(url, options = {}, timeout = 5000): Promise<Response> {
9
10 const ac = new AbortController();
11 const signal = ac.signal;
12
13 const timer = setTimeout(() => ac.abort(), timeout);
14
15 try {
16 return await fetch(url, { ...options, signal });
17 } catch (err) {
18 if (err.name === 'AbortError') {
19 throw new Error('Request timed out');
20 }
21 throw err;
22 } finally {
23 clearTimeout(timer);
24 }
25}
\No newline at end of file