【发布时间】:2019-03-03 15:06:39
【问题描述】:
用户将输入姓名电子邮件和订单信息,包括付款详情。 在单击表单中的“立即购买”按钮时,我计划执行以下步骤:
- 通过 api 调用创建用户。
- 自动登录并通过以下方式获取令牌 api 调用。
- 使用表单数据通过api调用创建订单。
- 通过 api 调用获取支付提供商令牌。
- 进入付款页面。
在前端使用 React-redux-saga。
请在下面的代码:
function* addToCartCamp(action) {
try {
// User creation and login
yield put({ type: authActions.AUTH_REGISTER_REQUEST, ...createUser });
yield put({ type: authActions.AUTH_REGISTER_SUCCESS, ...userdata });
yield put({ type: authActions.AUTH_LOGIN_REQUEST, ...login });
//Create order
const { data } = yield orderAPI.addToCartCamp(action);
yield put({ type: orderActions.ADD_TO_CART_SUCCESS, ...data });
yield put({ type: orderActions.GET_DETAIL_ORDER_REQUEST, ...{orderId: order_id} });
//Handle Payment
if(action.payment.method === 'creditCard'){
yield put({ type: orderActions.TOKEN_REQUEST, ...{orderId: order_id} });
} else{
yield put({ type: orderActions.BANK_TRANSFER_REQUEST, ...{orderId: order_id} });
}
} catch (error) {
// handle error message
}
}
我可以在 saga 文件中调用多个 Yield put,然后调用 api。调用此函数时,后端甚至在用户创建和登录之前就开始订单创建过程。
我需要所有进程同步运行,但它们当前以异步方式运行。
新的传奇和反应。怎么办?
【问题讨论】:
标签: reactjs synchronization react-redux yield redux-saga