title: How to pass additional arguments to callback functions in javascript when using .execute()?
body: <p>I'm working with javascript for Google Analytics API but I'm no more than a novice in JS. I have C++ and Java knowledge and I can get along with logical thinking but some things puzzle me. In GA API I get to make a function call like this:</p>
  
  <pre><code>gapi.client.analytics.data.ga.get({'ids':'&lt;tableID&gt;',
      'start-date':'&lt;startDate&gt;',
      'end-date':'&lt;endDate&gt;',
      'metrics':'&lt;metrics&gt;',
      'filters':'&lt;filters&gt;',
      'samplingLevel':'HIGHER_PRECISION',}).execute(putToVar);
  </code></pre>
  
  <p><code>putToVar()</code> is a user defined function defined like so:</p>
  
  <pre><code>function putToVar(results)
  {
      //do processing with the results
  }
  </code></pre>
  
  <p>It is my understanding that the <code>.execute()</code> method is used to invoke a callback function for the asynchronous call <code>gapi.client.analytics.data.ga.get()</code>. So I assume what <code>function1().execute(function2)</code> does is call <code>function2</code> with the return value from <code>function1</code> as argument? Is this correct?</p>
  
  <p>I'm in a situation where I need to apply several distinct filters and store them in an array to retrieve as and when required, irrespective of whether the API call returned a results object (it is an async call, so I don't know when the response comes, it is only visible to the callback function).</p>
  
  <p>I would like to pass to the callback function the dimensions of the array in which to store the returned objects so that I can retrieve them on demand later, without worrying about the order in which the responses get processed. I say this because, initially I tried a <code>for</code> loop and the order in which I got the response to my API calls were not the same as the order in which I placed API calls for my queries, so there were mismatches.</p>
  
  <p>Since the reference uses this method to invoke the callback function, I would like to know how to pass additional arguments to a callback function like this when using <code>.execute()</code> method, when I get to write <code>putToVar()</code> function something like this:</p>
  
  <pre><code>function putToVar(results,arrayDim)
  {
      //Process Results
      //Store in Array[arrayDim] the required value
  }
  </code></pre>
  
  <p>I hope I have made myself clear.
  I have read the following posts</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/494779/how-do-i-pass-multiple-arguments-into-a-javascript-callback-function">How do I pass multiple arguments into a javascript callback function?</a></li>
  <li><a href="https://stackoverflow.com/questions/18615285/passing-arguments-to-callback">passing arguments to callback</a></li>
  <li><a href="https://stackoverflow.com/questions/9596276/how-to-explain-callbacks-in-plain-english-how-are-they-different-from-calling-o">How to explain callbacks in plain english? How are they different from calling one function from another function?</a></li>
  <li><a href="https://stackoverflow.com/questions/17525688/how-to-pass-additional-arguments-to-callbacks-and-also-access-default-parameters">How to pass additional arguments to callbacks and also access default parameters?</a></li>
  </ul>
  
  <p>but none of them seem to use the <code>.execute()</code> method and I cannot figure out how to use what they have said. Or, if and how my <code>.execute()</code> method (type of callback execution) can be modified to help my purpose.</p>