简体   繁体   English

我的通话async / await返回Promise { <pending> }在我的行动中

[英]My call async/await returns a Promise {<pending>} in my actions

Hi I am very new to api calls and I am starting to use axios to get a simple deck of cards! 嗨,我对api调用非常陌生,我开始使用axios来获得简单的卡片组! I am trying to do a simple axios call, and when I console log my res, it gives me what I need. 我试图做一个简单的axios调用,当我用控制台记录我的资源时,它给了我所需的信息。 But when i return it, it gives me Promise { < pending > } . 但是当我退还它时,它给了我Promise {<未决>}。 From what I have seen, it is because it is a async promise, and the promise is getting resolved but because it is async, it moves on to the next line of code before returning the actual data? 从我所看到的,是因为它是一个异步promise,并且promise已得到解决,但是因为它是异步的,所以它在返回实际数据之前移到了下一行代码? Please correct me if I'm wrong! 如果我错了,请纠正我! Some clarification would be awesome. 一些澄清会很棒。 Thanks in advance! 提前致谢!

async function getData() {
    const result = await axios('https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1');
    console.log(result.data);
    return await result.data;
}

my actions in redux 我在redux中的动作

export function getNewDeck() {
    return {
        type: GET_NEW_DECK,
        payload: getData().then( res => {
                 return res.data
            })
    }
}

Then in my reducer, my action.payload returns the Promise { < pending > } 然后在我的化简器中,我的action.payload返回Promise {<未决>}

Updated from comments 从评论更新

I believe you just need to change your export function to also be asynchronous and await on getData: 我相信您只需要将导出功能更改为异步并等待getData:

export async function getNewDeck() {
    const payload = await getData();
    return {
        type: GET_NEW_DECK,
        payload
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM