如何多次解析响应



所以我有一个函数,它会返回一个值例如:

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}

^^这是secound文件中的第一个文件,我想获得使用它们的值,这里的问题是,在我第一次使用函数时:

const first_file = require("./first_file") 
first_file.then(res=>{
console.log(res)
})

我第一次使用该函数时,一切都很好,但有时如果出现错误,我会使用try-catch方法再次调用该函数,这将返回我第一次时得到的相同响应

抱歉的解释不正确

我不知道你想要什么。但是,如果电话崩溃,你可能不应该再这样做了。但是,如果你想在呼叫崩溃时做任何事情,你可以在.then之后使用.catch

编辑函数value以返回axios调用。

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

然后你可以这样访问它:

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) 
})

最新更新