我是反应 js 的初学者,我正在开发一个经常发出 api 请求的小应用程序。所以我面临的问题是有一个页面包含从数据库预填充的表单字段,如果用户对这些字段进行更改,我会将新提交的值发布到数据库。单击提交按钮时,将调用 saveAndConttinue(),然后根据条件调用 addNewAddress()。但问题是我从 addNewAddress 获得的响应必须用于队列中的下一个 api 调用,但是获取响应需要时间,并且 address_id 的 post 调用具有空值。现在有什么方法可以在不使用flux/redux的情况下进行同步调用?
saveAndContinue(e) {
e.preventDefault();
if(this.props.params.delivery === 'home_delivery' && this.state.counter) {
this.addNewAddress();
}
console.log('add id is '+this.state.address_id);
const config = { headers: { 'Content-Type': 'multipart/form-data' } };
let fd = new FormData();
fd.append('token', this.props.params.token);
fd.append('dish_id', this.props.params.dish_id);
fd.append('address_type', this.props.params.delivery);
fd.append('address_id', this.state.address_id);
fd.append('ordered_units', this.props.params.quantity);
fd.append('total_cost', this.props.params.total_cost);
fd.append('total_service_charge', this.props.params.service_charge);
fd.append('net_amount', this.props.params.net_cost);
fd.append('hub_id', this.props.params.hub_id);
fd.append('delivery_charge', this.props.params.delivery_charge);
fd.append('payment_type', this.state.payment_type);
fd.append('device_type', 'web');
axios.post(myConfig.apiUrl + '/api/foody/orders/purchase' , fd, config)
.then(function(response){
if(response.data.success) {
console.log(response);
browserHistory.push('/order_confirmed/'); …Run Code Online (Sandbox Code Playgroud)