@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.
32 lines (31 loc) • 936 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* A helper to assign timeouts to fetch or other promises
* Useful in nextjs middleware until fetch.signal is fully supported by Vercel edge functions
*/
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);
}
}
exports.default = TimeoutPromise;