所以我创建了一个组件,通过我提供的props发送post请求。
虽然我熟悉Async await,但由于某种原因似乎无法返回实际兑现承诺的价值,而只是得到待定状态。
我尝试用更多的函数包装,因为我理解承诺没有被解决。
感觉我漏掉了什么。
以下是我的代码示例
export default class PostController extends React.Component {
constructor(props) {
super(props)
}
Wrapper = (body) => {
try{
let x = this.Send(body);
return x;
console.log(x)
}catch(e){console.log(e)}
}
Send = async (body) => {
try{
let data = await this.Post(body);
return data;
}catch(e){console.warn(e)}
}
Post = async (body) => {
try{
const options = {
method: 'POST',
uri: 'XXXXXXXXXXXXXXXXXXXX',
body: body
}
const data = await rp(options);
return data;
}catch(e){console.warn(e)}
}
render(props) {
let jsonBody = JSON.stringify(this.props.data)
const returnData = this.Wrapper(jsonBody)
console.log(returnData)
return(
<div>
{(!this.props.data.pw) ? 'Retrieved Password: ' + returnData.message : 'Generated PassWord!:' + returnData.message }
</div>
)
}
rp(options)
是什么,以及为什么它需要等待某些东西吗?... - Arfeo