reactjs / flux:按顺序执行操作(ajax)
reactjs / flux: execute actions (ajax) in order
我的通量操作只是 AJAX 调用,成功时用数据更新商店。
在我的组件中,我有一个使用这些操作的函数:
// Insert new event
ReactActions.submitNewEvent(data)
// Insert new exceptions
ReactActions.submitNewExceptions(exception_objects)
// Merge
ReactActions.merge(data, exception_objects)
我想完全执行前两个操作(意味着 ajax 方法的 200 成功响应)在第三个操作之前执行。
在类似 Flux 的架构中执行此操作的最佳方法是什么?我应该选择承诺吗?你能举个例子吗?
是的,promises 是解决此类问题的好方法。
Promise.all()
将帮助您将最后一次通话与前两次通话同步。确保 submitNewEvent()
和 submitNewExceptions()
return 在 AJAX 调用完成时解决的承诺:
Promise.all([
ReactActions.submitNewEvent(data),
ReactActions.submitNewExceptions(exception_objects)
]).then(function() {
ReactActions.merge(data, exception_objects)
})
我的通量操作只是 AJAX 调用,成功时用数据更新商店。
在我的组件中,我有一个使用这些操作的函数:
// Insert new event
ReactActions.submitNewEvent(data)
// Insert new exceptions
ReactActions.submitNewExceptions(exception_objects)
// Merge
ReactActions.merge(data, exception_objects)
我想完全执行前两个操作(意味着 ajax 方法的 200 成功响应)在第三个操作之前执行。
在类似 Flux 的架构中执行此操作的最佳方法是什么?我应该选择承诺吗?你能举个例子吗?
是的,promises 是解决此类问题的好方法。
Promise.all()
将帮助您将最后一次通话与前两次通话同步。确保 submitNewEvent()
和 submitNewExceptions()
return 在 AJAX 调用完成时解决的承诺:
Promise.all([
ReactActions.submitNewEvent(data),
ReactActions.submitNewExceptions(exception_objects)
]).then(function() {
ReactActions.merge(data, exception_objects)
})