Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 6 additions & 140 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 24 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,30 @@ function logTracingHeader(response: Response) {
log.info(`Request tracing with traceID=${response.headers.get("x-web3-correlation-id")}`);
}

export const promiseTimeout = <T>(ms: number, promise: Promise<T>): Promise<T> => {
const timeout = new Promise<T>((_resolve, reject) => {
const id = setTimeout(() => {
clearTimeout(id);
reject(new Error(`Timed out in ${ms}ms`));
}, ms);
});
return Promise.race<T>([promise, timeout]);
export const promiseTimeout = async <T>(ms: number, promise: Promise<T>): Promise<T> => {
let timeoutFunc: ReturnType<typeof setTimeout> | null = null;
try {
const timeout = new Promise<T>((_resolve, reject) => {
timeoutFunc = setTimeout(() => {
reject(new Error(`Timed out in ${ms}ms`));
}, ms);
});

const result = await Promise.race<T>([promise, timeout]);
// promise.race will return the first resolved promise
// then we clear the timeout
if (timeoutFunc != null) {
clearTimeout(timeoutFunc);
}
return Promise.resolve(result);
} catch (err) {
// clear the timeout
if (timeoutFunc != null) {
clearTimeout(timeoutFunc);
}
// rethrow the original error
throw err;
}
};

export const get = async <T>(url: string, options_: RequestInit = {}, customOptions: CustomOptions = {}) => {
Expand Down
8 changes: 7 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"extends": "@toruslabs/config/tsconfig.default.json",
"include": ["src"]
"include": ["src"],
"ts-node": {
"compilerOptions": {
"module": "CommonJS",
"target": "ES2017"
}
}
}