我正在异步检索密码,并且工作正常:
static async login(){
let password = await User.getPassword();
alert(password); // this works, password is shown
但是如果我想在 fetch 中使用它,它就不起作用
static async login(){
let password = await User.getPassword();
alert(password); // this works, password is shown
return fetch(userInfoURL,
{ method: 'GET',
headers: { 'Authorization': password,
'Content-Type': 'application/json'}
})
服务器收到的密码为空
我也尝试过
User.getPassword().then((password) => {
return fetch(userInfoURL,
{ method: 'GET',
headers: { 'Authorization': password,
'Content-Type': 'application/json'}
})
但是我也遇到了同样的问题,密码为空。
最佳答案
您必须等待获取
return await fetch(userInfoURL,
{ method: 'GET',
headers: { 'Authorization': password,
'Content-Type': 'application/json'}
})
关于javascript - 使用await 与react-native 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48207413/