how to resolve a response multiple times

Viewed 320

so i have a fucntion which would return a Value for example :

let  value= function () =>{
//basically getting values from an api 

let get = axios.get("example.com").then(res=>{
Value = res
})
//     Value is the value that had been retrieved from a  Get request from an API 
return Value ; 

}
module.export = {value}

^^ That is the first file in the secound file i want to get the values of use the them , the problem here that in the first time i use the function :

const first_file = require("./first_file") 

first_file.then(res=>{
console.log(res)
})

the first time i use the fucntion very thing would be fine but sometimes in case of an error i would call the function again using try catch method , this would return the same response i got first time

Sorry For the bad explanation

1 Answers

I am not sure to understand what you want. But if a call crash probably you shouldn't do it again. But if you want to do something whatever it is when your call crash you can use .catch after .then.

Edit your function value to return the axios call.

let value = function () => {
     return axios.get("example.com")
}

module.export = {value}

Then you can access it like this :

const first_file = require("./first_file") 

first_file.value.then(res=>{
    console.log(res)
}).catch(error => {
// For exemple you can log the error, or do whatever you want here
   console.log(error) 
})

Related