0

I create a function asyncFunc which makes an asynchronous request. How can I return the response/result from asyncFunc and then save the result into a variable to use later or to print to console? below is what I have so far. Any help will be greatly appreciated.

var axios = require('axios');

async function asyncFunc() {
    // fetch data from a url endpoint
    const data = await axios.get("/some_url_endpoint");
    return data;
}

var text = asyncFunc()
console.log(text)

this is my result when I log to console: Promise { }

  • 2
    `asyncFunction().then(response => console.log(response.data));` – blex Dec 13 '20 at 17:36
  • In case you're interested, [here](https://exploringjs.com/es6/ch_promises.html) is a good reference to read more about Promises for asynchronous programming. (You might want to read [this](https://exploringjs.com/es6/ch_async.html) first). – fardjad Dec 13 '20 at 17:40
  • I will really like to save the results into a variable so that I can use it later for something else... examples pass the results into another function, write to a database, perform a filter or post the results into another API. – Kingsley.rock Dec 13 '20 at 17:46

0 Answers0