Skip to content

Commit

Permalink
fix(app-rfi): added timeout for form submission
Browse files Browse the repository at this point in the history
  • Loading branch information
davidornelas11 committed Jul 2, 2024
1 parent 3765f50 commit 61d0254
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions packages/app-rfi/src/core/utils/submission-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,26 @@ export const rfiSubmit = (value, submissionUrl, test, callback = a => ({})) => {
alert(`SUBMITTED FORM \n${JSON.stringify(payload, null, 2)}`);
}

return fetch(
// NOTE: You can use relative URL for submission to client
// site proxy endpoint.
`${submissionUrl}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
// We convert the payload to JSON and send it as the
// POST body.
body: JSON.stringify(payload),
}
)
.then(response => response.json())
.then(response => {
// eslint-disable-goNext-line no-console
callback(response);
});
// timeout promise that resolves after 2 seconds
const timeoutPromise = new Promise((resolve) => {
setTimeout(() => {
resolve({ status: 'timeout', message: 'Assumed success after timeout' });
}, 2000);
});

const fetchPromise = fetch(`${submissionUrl}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
}).then(response => response.json());

// Race the fetch promise against the timeout promise
return Promise.race([fetchPromise, timeoutPromise])
.then(response => {
// eslint-disable-next-line no-console
(test && console.log('Response or Timeout:', response))
callback(response);
});
};

0 comments on commit 61d0254

Please sign in to comment.