Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0125· OpenAIAi2 WildChat-4.8M (AI2)

I have this code to get response from search api const searchResponse = await…

  1. 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

  2. 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 customFetch function 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. The timeout variable 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.

  3. User

    but i need to use searchPromise and timeoutPromise

  4. 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 searchRequest variable holds the fetch request to the '/search' endpoint with the specified options. Then, a timeoutPromise is created using setTimeout to reject the promise with an error after 10 minutes.

    Next, the Promise.race function is used to race between the searchRequest and timeoutPromise. Whichever promise settles first will determine the outcome of the overall promise. If the search request resolves before the timeout, the searchResponse will contain the response data, otherwise the timeoutPromise will reject with an error.

    This way, you can handle the search request and timeout separately using two different promises.