0

I need the results of the array outside of the function. Is there anyway to do that?

I am trying to push the results of a json object to an array to use.

let chan = [];
axios.get(names, {
    cancelToken: new CancelToken(function executor(c) {
      api_requests.nm = c;
    })
  })
  .then(function(name) {
    chan = name.data.map(d => d.elem);
    return chan;
  }).catch(function(error) {
    console.log(error);
    alert_error(error);
  });

I want to use the results of chan outside of this function globally. Is there anyway to do that?

nb_nb_nb
  • 1,243
  • 11
  • 36
  • You probably look for async await , then you can be able to use the result in your async function only ,coz return from async function will wrap it into promise – Shubham Dixit Oct 17 '19 at 15:18
  • Can you please give an example? – nb_nb_nb Oct 17 '19 at 15:19
  • Look at this -https://stackoverflow.com/questions/49474073/using-axios-with-async-and-await – Shubham Dixit Oct 17 '19 at 15:21
  • @Shubh, I keep getting errors, I tried what was suggested in that post. Is there a way to try and help me debug my code? – nb_nb_nb Oct 17 '19 at 15:47
  • You probably ,should see the axios docs correctly ,otherwise there are other libraries like npm request which are made for this tasks and have good documentation too. – Shubham Dixit Oct 17 '19 at 16:01

0 Answers0