如何多次解决响应
how to resolve a response multiple times
所以我有一个函数可以 return 一个值
例如:
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}
^^ 这是第二个文件中的第一个文件,我想获取使用它们的值,这里的问题是我第一次使用该函数:
const first_file = require("./first_file")
first_file.then(res=>{
console.log(res)
})
我第一次使用该函数时一切正常,但有时如果出现错误,我会使用 try catch 方法再次调用该函数,这将 return 与我第一次得到的响应相同
抱歉解释不当
我不确定你想要什么。但是,如果通话崩溃,您可能不应该再这样做。
但是如果你想在通话崩溃时做任何事情,你可以在 .then
.
之后使用 .catch
将函数 value
编辑为 return 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)
})
所以我有一个函数可以 return 一个值 例如:
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}
^^ 这是第二个文件中的第一个文件,我想获取使用它们的值,这里的问题是我第一次使用该函数:
const first_file = require("./first_file")
first_file.then(res=>{
console.log(res)
})
我第一次使用该函数时一切正常,但有时如果出现错误,我会使用 try catch 方法再次调用该函数,这将 return 与我第一次得到的响应相同
抱歉解释不当
我不确定你想要什么。但是,如果通话崩溃,您可能不应该再这样做。
但是如果你想在通话崩溃时做任何事情,你可以在 .then
.
.catch
将函数 value
编辑为 return 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)
})