Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
Passing external variables into Axios.get().then() call

I want to call an endpoint using Axios, then do something with the response data and a variable that was defined before the call. I know how to pass data into the callback when creating the function returning the promise myself, but since Axios is external code and I can't seem to find any option to pass extra data in the Axios docs or the config. Is there any other way to make this work, other than sending the value as data to the server and have the server respond it within the response or using an ugly workaround using the window global variable?

const animate = true; // The variable to be passed
axios.get('/endpoint.json')
    .then(function(response, animate) { // This doesn't work...
        if (response.data.coordinates) {
            console.log(animate); // ...because this is undefined
            setLocation('takeoff_location', response.data.coordinates, animate); // variable and response data need to be passed to this call
        }
    });
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This creates a new animate variable, entirely unrelated to the one already defined:

function(response, animate)

This new variable "shadows" the one in the higher scope, so within this function any reference to animate only references this new variable. Which isn't defined because Axios (specifically the Promise returned by .get()) has no reason to pass a second value to this callback.

Don't shadow the variable:

function(response)

Then within the function any references to animate will reference the variable in the higher scope.

about 4 years ago · Juan Pablo Isaza Report

0

the .then() is a promise and has access to any variables outside the scope (like a closure). If you want to encapsulate the API call then wrap the entire thing in a function and the arguments go there.

const doRequest = function(animate) {
  axios.get('/endpoint.json').then(function(response) {
    if (response.data.coordinates) {
      setLocation('takeoff_location', response.data.coordinates, animate);
    }
  });
};

doRequest(250);
doRequest(500);

Note that this will do 2 animation requests quickly in succession. If you want to "wait" for the other one to finish first, you would do this: (note that I'm now returning the axios request from the function). This means the promise object is returned and now you can chain more .then() functions on the "outside" of the function, which will happen only when the request finishes.

const doRequest = function(animate) {
  return axios.get('/endpoint.json').then(function(response) {
    if (response.data.coordinates) {
      setLocation('takeoff_location', response.data.coordinates, animate);
    }
  });
};

doRequest(250).then(function() {
  // this will happen when the outer API call finishes
  doRequest(500);
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!