如何兑现承诺';已经';然后';在Javascript/ReactNative中?
使用以下代码:如何兑现承诺';已经';然后';在Javascript/ReactNative中?,javascript,react-native,asynchronous,Javascript,React Native,Asynchronous,使用以下代码: makeRequest = async (path, body) => { let client = await this.getClient() return this.client.post(path, body).then((response) => { console.log("Response on NetModule.makeRequest: ", response) if (!respo
makeRequest = async (path, body) => {
let client = await this.getClient()
return this.client.post(path, body).then((response) => {
console.log("Response on NetModule.makeRequest: ", response)
if (!responseIsSuccess(response)) { toast("The response shows some errors.") }
return response
}).catch((error) => {
toast("The request failed")
})
}
login = async (username, password) => {
let body = { username: username, password: password)
return this.makeRequest('/login', body).then((response) => {
console.log("Response on NetModule.login: ", response)
if (responseIsSuccess(response)) { saveResponseToDisk(response); }
return response
})
}
btnLoginClick = async () => {
const { username, password } = this.state
NetModule.login(username, password).then((response) => {
console.log("Response on LoginScreen.btnLoginClick: ", response)
if (responseIsSuccess(response)) { this.props.navigation.navigate(home) }
return response
})
}
答复是:
Response on NetModule.login: undefined
Response on LoginScreen.btnLoginClick: undefined
Response on NetModule.makeRequest: <has response>
NetModule.login上的响应:未定义
LoginScreen.btnLoginClick上的响应:未定义
对NetModule.makeRequest的响应:
为什么会这样?我如何才能做出这样的回答(并按此顺序):
NetModule.makeRequest上的响应:
NetModule.login上的响应:
在LoginScreen.btnlogin上的响应单击:
不要混搭
async/await
和。如果不需要的话,那么就混搭。当前的问题是btnLoginClick
不会“等待”NetModule.login
完成。它立即返回<代码>返回NetModule。登录将解决此问题,但我们可以做得更好:
makeRequest = async (path, body) => {
let client = await this.getClient()
try {
let response = await client.post(path, body)
console.log("Response on NetModule.makeRequest: ", response)
if (!responseIsSuccess(response)) { toast("The response shows some errors.") }
return response
} catch (error) {
toast("The request failed")
}
}
login = async (username, password) => {
let body = { username: username, password: password)
let response = await this.makeRequest('/login', body)
console.log("Response on NetModule.login: ", response)
if (responseIsSuccess(response)) { saveResponseToDisk(response); }
return response
}
btnLoginClick = async () => {
const { username, password } = this.state
let response = await NetModule.login(username, password);
console.log("Response on LoginScreen.btnLoginClick: ", response)
if (responseIsSuccess(response)) { this.props.navigation.navigate(home) }
return response
}
不要混用async/await
和。如果不需要,那么就混用。当前的问题是btnLoginClick
不会“等待”NetModule.login
完成。它立即返回<代码>返回NetModule。登录将解决此问题,但我们可以做得更好:
makeRequest = async (path, body) => {
let client = await this.getClient()
try {
let response = await client.post(path, body)
console.log("Response on NetModule.makeRequest: ", response)
if (!responseIsSuccess(response)) { toast("The response shows some errors.") }
return response
} catch (error) {
toast("The request failed")
}
}
login = async (username, password) => {
let body = { username: username, password: password)
let response = await this.makeRequest('/login', body)
console.log("Response on NetModule.login: ", response)
if (responseIsSuccess(response)) { saveResponseToDisk(response); }
return response
}
btnLoginClick = async () => {
const { username, password } = this.state
let response = await NetModule.login(username, password);
console.log("Response on LoginScreen.btnLoginClick: ", response)
if (responseIsSuccess(response)) { this.props.navigation.navigate(home) }
return response
}
然后还回一个承诺。@SijuSamson你是什么意思?像returnnewpromise((resolve,reject)=>resolve(response))
?@ChenLiYong您需要return wait
在login
和makeRequest
@muhammaddatalhaakbar中,您可以从异步函数返回承诺。等待是没有必要的。然而,这是一个迹象,表明他不必要地包装承诺,使事情变得更加复杂。然后他也会返回一个承诺。@SijuSamson你是什么意思?像returnnewpromise((resolve,reject)=>resolve(response))
?@ChenLiYong您需要return wait
在login
和makeRequest
@muhammaddatalhaakbar中,您可以从异步函数返回承诺。等待是没有必要的。然而,这是一个迹象,表明他不必要地包装承诺,使事情变得更复杂。有趣。我尽量避免wait
,因为有时它会占用线程流,也因为.catch
子句,但也许我会尝试使用try。。。改为catch
方法。谢谢。使用async
函数的关键在于能够在其中使用wait
。如果不使用wait
,则不必将函数声明为async
。这或多或少只是的语法糖分。那么无论如何,
很有趣。我尽量避免wait
,因为有时它会占用线程流,也因为.catch
子句,但也许我会尝试使用try。。。改为catch
方法。谢谢。使用async
函数的关键在于能够在其中使用wait
。如果不使用wait
,则不必将函数声明为async
。这或多或少只是的语法糖分。那么无论如何
。