diff --git a/package.json b/package.json index 160a1325c..02a6a3d82 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@snapshot-labs/snapshot.js", - "version": "0.5.6", + "version": "0.5.7", "repository": "snapshot-labs/snapshot.js", "license": "MIT", "main": "dist/snapshot.cjs.js", diff --git a/src/utils.ts b/src/utils.ts index 2744ded4f..9d68d78be 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -135,8 +135,18 @@ export async function multicall( } } +export function fetchWithTimeout(url, options): Promise { + const timeout = options.timeout || 30000; + return Promise.race([ + fetch(url, options), + new Promise((_, reject) => + setTimeout(() => reject(new Error('Request timeout')), timeout) + ) + ]); +} + export async function subgraphRequest(url: string, query, options: any = {}) { - const res = await fetch(url, { + const res = await fetchWithTimeout(url, { method: 'POST', headers: { Accept: 'application/json', @@ -470,6 +480,7 @@ export function getNumberWithOrdinal(n) { export default { call, multicall, + fetchWithTimeout, subgraphRequest, ipfsGet, getUrl,