title: How to speed up a function call in nodejs
body: <p>I'm creating a get request to the backend as soon certain data is posted to the database. So in other to wait for the post request to be completed before a get request is fired, I implemented a waiting logic like so, <code>postArray.called = true</code></p>
  <pre><code>const postArray = async (arr) =&gt; {
      const movie = {
          name: searchTerm,
          result: arr
      }
      try {
          await axios({
              method: 'post',
              url: url,
              data: result,
              headers:{
                  &quot;Content-Type&quot;:&quot;application/json&quot;,
                  &quot;Accept&quot;:&quot;application/json&quot;
                }
              })
      } catch (error) {
          console.log(error)
      }
      postArray.called = true
  }
  </code></pre>
  <p>To make the get request wait for the post request, I used a if condition like so.</p>
  <pre><code>app.get(&quot;/getNewList&quot;, (req, res)=&gt;{
      if(postArray.called){
          process.nextTick(()=&gt;{
              axios.get('http://localhost:3001/List')
                  .then(response =&gt; res.send(response.data))
                  .catch(error =&gt; res.send(error))
          })
      }
  })
  </code></pre>
  <p>The problem is, this get request doesn't get call for another 2 or 3 minutes after the post request has been called. I need a way to speed up this function call so it doesn't slow my app down completely.</p>
  <p>Sometimes, the request times out and I get a timeout error.</p>
  <p>Is there another way to make this quicker?</p>
  <p>////EDIT////</p>
  <p>I have been informed that using <code>if(postArray.called)</code> is not the same as making the get request wait, so I tried something else.</p>
  <pre><code> async function getData(props){
          let Result = []
          let fetchAgain = await axios.get('/getNewList')
          Result = fetchAgain.data.find(x =&gt; x.name.toLowerCase() === props.toLowerCase()).result
          showResult(Result)
      }
  
   async function fetchPost(props){
        axios.post(`/${props}`)
          .then((res) =&gt;{
              if(res.status === 200){
                  getData(props)
              }
          }) 
      }
  </code></pre>
  <p>This is the new request made from the front end. I declared a function to call the get request, then passed it to the <code>.then</code> part of the post request.
  this still doesn't work. Keep in mind that <code>if(postArray.called)</code> was removed from the get request on the backend</p>
  <hr />
  <p>The <code>fetchPost</code> function was used to send data to my backend which then queries two external APIs:</p>
  <pre><code>function newFetch(a){
      const promises = [
    Fetch(`https://externalapi1?=${a}.com`,
    Fetch(`https://externalapi2?=${a}.com`,]
      
      Promise.all(promises)
          .then(values =&gt;{
              const result = values.filter(o=&gt;Object.values(o).every(v=&gt;v) &amp;&amp; Object.entries(o).length);
  
              postArray(result)}
          .catch(err =&gt; console.log(err))
  }
  </code></pre>
  <p>This is where the postArray function is called. Then I intend to fetch this data from the backend to render to my client. Using both failed methods listed above</p>
  <p>///End EDIT///</p>
  <p>/////This is the place where newFetch was called////</p>
  <pre><code>app.post(&quot;/:endpoint([\\/\\w\\ ]*)&quot;, async (req, res) =&gt;{
      newFetchPromise = await newFetch(req.params.endpoint).catch(err =&gt; {
          newFetchPromise = null
      })
  })
  </code></pre>