gpt4 book ai didi

javascript - 使用 async 和 await with export const

转载 作者:数据小太阳 更新时间:2023-10-29 04:06:44 25 4
gpt4 key购买 nike

我无法完成这项工作...它说:await 是一个保留字。是的,当然是……而且我想使用它:)

怎么了?

export const loginWithToken = async () => {
return dispatch => {
dispatch({type: SESSION_LOGIN_IN_PROGRESS, payload: true})
let storedData = await ReadFromLocalDB('user')
console.log(storedData)
if (!storedData) {
invalidToken(null, dispatch)
}
else {
storedData = JSON.parse(storedData)
SessionLoginWithToken(storedData.session.token).then(res => {
console.log(res)
loginSuccessfully(res, dispatch, true)
})
}
}
}

我的 ReadFromLocalDB 函数如下所示:

export const ReadFromLocalDB = async (key) => {
return AsyncStorage.getItem(key)
}

它返回一个 promise

最佳答案

return dispatch => {...} 我相信也需要 async。现在,只有顶级函数是 async,而不是嵌套函数。

// This function is async
export const loginWithToken = async () => {
// This one is not though which means it can't use await inside
// return dispatch => {

// Instead it should likely be:
return async dispatch => {
dispatch({type: SESSION_LOGIN_IN_PROGRESS, payload: true})
let storedData = await ReadFromLocalDB('user')
console.log(storedData)
if (!storedData) {
invalidToken(null, dispatch)
}
else {
storedData = JSON.parse(storedData)
SessionLoginWithToken(storedData.session.token).then(res => {
console.log(res)
loginSuccessfully(res, dispatch, true)
})
}
}
}

关于javascript - 使用 async 和 await with export const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50804207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com