如何兑现承诺';已经';然后';在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
。这或多或少只是
的语法糖分。那么无论如何