Javascript Axios返回待定承诺

Javascript Axios返回待定承诺,javascript,promise,async-await,axios,Javascript,Promise,Async Await,Axios,我希望这个函数返回true或false,而不是 /** * Sends request to the backend to check if jwt is valid * @returns {boolean} */ const isAuthenticated = () => { const token = localStorage.getItem('jwt'); if(!token) return false; const config = {heade

我希望这个函数返回true或false,而不是

/**
 * Sends request to the backend to check if jwt is valid
 * @returns {boolean} 
 */
const isAuthenticated = () => {
    const token = localStorage.getItem('jwt'); 
    if(!token) return false; 
    const config = {headers : {'x-auth-token' : token}}; 

    const response = axios.get('http://localhost:8000/user' , config)
    .then(res =>  res.status === 200 ? true : false)
    .catch(err => false);

    return  response;
}   

export default isAuthenticated; 
我尝试分离它们并使用async/await:

const isAuthenticated = async () => {
    const response = await makeRequest();
    return  response;
}   


const makeRequest = async () => { 
    const token = localStorage.getItem('jwt'); 
    const config = {headers : {'x-auth-token' : token}}; 
    const response = await axios.get('http://localhost:8000/user' , config)
    .then(res =>  res.status === 200 ? true : false)
    .catch(err => false);

    return response;
}
而且还是一样

在提出一些建议之后:

const isAuthenticated =  () => {
    const response =  makeRequest();
    return  response;
}   


const makeRequest = async () => { 
    try {
        const token = localStorage.getItem('jwt'); 
        const config = {headers : {'x-auth-token' : token}}; 
        const response = await axios.get('http://localhost:8000/user', config);
        if (response.status === 200) { // response - object, eg { status: 200, message: 'OK' }
            console.log('success stuff');
            return true;
        }
        return false;
   } catch (err) {
        console.error(err)
        return false;
   }
}
export default isAuthenticated; 
首先,如果。 如果您使用默认的promise-then&catch,那么应该在“then”函数中处理成功操作

axios.get('http://localhost:8000/user', config)
.then(res => console.log('succesfull stuff to be done here')
.catch(err => console.error(err)); // promise
如果您想使用async/await语法糖,我个人喜欢它

const makeRequest = async () => { 
    try {
    const token = localStorage.getItem('jwt'); 
    const config = {headers : {'x-auth-token' : token}}; 
    const response = await axios.get('http://localhost:8000/user', config);
    if (response.status === 200) { // response - object, eg { status: 200, message: 'OK' }
      console.log('success stuff');
     return true;
    }
    return false;
   } catch (err) {
     console.error(err)
     return false;
   }
}

等待
~
那么
是一个反模式,你应该检查一下&它注销了成功的东西,但我对这个真值或假值感兴趣,它没有返回真值或假值,但仍然挂起。我正在做
console.log(makeRequest())
如果这是直截了当的,我只是不明白,我向您道歉您打算采取什么方法?promise.then.catch或async/await?如果您使用的是promise.then.catch,那么您不能简单地控制台.log(promise),您将得到挂起。在promise.then()中处理状态===200的异步等待,如您发布的示例所示。我将用我尝试过的内容更新问题,以便您可以看到。如果您这样做
。那么(res=>res.status==200?true:false)
这意味着您将返回布尔值
true
false
。您可以使用
.then(res=>res.status==200?true:false)捕捉到这一点。然后(boolValue=>{console.log(boolValue)})
您现在应该可以在控制台中看到true或false。我希望您熟悉该语法
(res=>someValue)
(res=>{returnsomevalue;})