Skip to content

Commit

Permalink
Create deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
blittle committed Aug 26, 2024
1 parent 0933f81 commit b0ca484
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
36 changes: 36 additions & 0 deletions templates/skeleton/app/lib/timeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const defaultNetworkTimeoutTime = 5000; // ms

export type PromiseWithTimeoutProps<T> = {
promise: Promise<T>;
timeout?: number;
errorCallback?: (error: string) => void;
};

export const promiseWithTimeout = <T>({
promise,
timeout = defaultNetworkTimeoutTime,
errorCallback,
}: PromiseWithTimeoutProps<T>): Promise<T> => {
let timer: NodeJS.Timeout;

return Promise.race([
promise,
new Promise<T>((_, reject) => {
timer = setTimeout(() => {
console.log('a');
reject(new Error('Request timed out'));
}, timeout);
}),
])
.then((data) => {
console.log('b');
clearTimeout(timer);
return data;
})
.catch((error) => {
console.log('c');
clearTimeout(timer);
errorCallback && errorCallback(error);
return new Promise<T>((_, reject) => reject(error));
});
};
7 changes: 7 additions & 0 deletions templates/skeleton/app/routes/products.$handle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {getVariantUrl} from '~/lib/variants';
import {ProductPrice} from '~/components/ProductPrice';
import {ProductImage} from '~/components/ProductImage';
import {ProductForm} from '~/components/ProductForm';
import {promiseWithTimeout} from '~/lib/timeout';

export const meta: MetaFunction<typeof loader> = ({data}) => {
return [{title: `Hydrogen | ${data?.product.title ?? ''}`}];
Expand Down Expand Up @@ -43,6 +44,12 @@ async function loadCriticalData({
throw new Error('Expected product handle to be defined');
}

console.log('wow');

await promiseWithTimeout({
promise: new Promise((resolve) => setTimeout(resolve, 6000)),
});

const [{product}] = await Promise.all([
storefront.query(PRODUCT_QUERY, {
variables: {handle, selectedOptions: getSelectedProductOptions(request)},
Expand Down

0 comments on commit b0ca484

Please sign in to comment.