@sitecore-jss/sitecore-jss
Version:
This module is provided as a part of Sitecore JavaScript Rendering SDK. It contains the core JSS APIs (layout service) and utilities.
29 lines (28 loc) • 840 B
JavaScript
/**
* A helper to assign timeouts to fetch or other promises
* Useful in nextjs middleware until fetch.signal is fully supported by Vercel edge functions
*/
export default class TimeoutPromise {
constructor(timeout) {
this.timeout = timeout;
this.timeoutId = undefined;
}
/**
* Creates a timeout promise
*/
get start() {
return new Promise((_, reject) => {
this.timeoutId = setTimeout(() => {
const abortError = new Error(`Request timed out, timeout of ${this.timeout}ms is exceeded`);
abortError.name = 'AbortError';
reject(abortError);
}, this.timeout);
});
}
/**
* Clears the timeout from timeout promise
*/
clear() {
this.timeoutId && clearTimeout(this.timeoutId);
}
}