I have this code to get response from search api const searchResponse = await…
- User
I have this code to get response from search api const searchResponse = await fetch('/search', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: userRequestTextareaContent + '\n' + userSearchClarification, use_llm_for_teasers: enableLLMForTeasers, do_multistep_search: doMultistepSearch }) }); But i`m getting HeaderTimeoutError How can i provide custom timout with value of 10 minutes using promise
- Assistant
You can provide a custom timeout value for the fetch request by creating a new Promise and using the setTimeout function to handle the timeout. Here's an example of how you can do this:
const customFetch = async (url, options, timeout) => { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), timeout); const response = await fetch(url, { ...options, signal: controller.signal }); clearTimeout(timeoutId); return response; }; const timeout = 10 * 60 * 1000; // 10 minutes in milliseconds try { const searchResponse = await customFetch('/search', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: userRequestTextareaContent + '\n' + userSearchClarification, use_llm_for_teasers: enableLLMForTeasers, do_multistep_search: doMultistepSearch }) }, timeout); // Handle the search response here } catch (error) { if (error.name === 'AbortError') { console.error('Request timed out'); } else { console.error('Error fetching data:', error); } }In this code snippet, the
customFetchfunction is created to handle the fetch request with a custom timeout value. The function uses an AbortController to cancel the request if the timeout occurs. Thetimeoutvariable is set to 10 minutes in milliseconds. You can adjust this value according to your requirements. The fetch request is then made using this custom fetch function, and the response is handled as needed. - User
but i need to use searchPromise and timeoutPromise
- Assistant
You can achieve this by creating two separate promises: one for the search request and one for the timeout. Here's an example of how you can do this:
const searchRequest = fetch('/search', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: userRequestTextareaContent + '\n' + userSearchClarification, use_llm_for_teasers: enableLLMForTeasers, do_multistep_search: doMultistepSearch }) }); const timeoutPromise = new Promise((resolve, reject) => { setTimeout(() => reject(new Error('Request timed out')), 10 * 60 * 1000); // 10 minutes timeout }); try { const searchResponse = await Promise.race([searchRequest, timeoutPromise]); // Handle the search response here } catch (error) { console.error(error); }In this code snippet, the
searchRequestvariable holds the fetch request to the '/search' endpoint with the specified options. Then, atimeoutPromiseis created usingsetTimeoutto reject the promise with an error after 10 minutes.Next, the
Promise.racefunction is used to race between thesearchRequestandtimeoutPromise. Whichever promise settles first will determine the outcome of the overall promise. If the search request resolves before the timeout, thesearchResponsewill contain the response data, otherwise thetimeoutPromisewill reject with an error.This way, you can handle the search request and timeout separately using two different promises.