保证调度顺序
Guaranteed Dispatch Sequence
我正在尝试寻找有关 redux reducer 的信息,但我认为术语可能有所不同,因为这是 JavaScript。
假设以下减速器:
import {
ONE,
TWO,
THREE
} from "../actions/types";
export default function reducer(state = { /* some init state here */ }, action) {
switch (action.type) {
case ONE: {
// this case takes 500ms to finish
return {...state }
}
case TWO: {
// this case takes 200ms to finish
return {...state }
}
case THREE: {
// this case takes 100ms to finish
return {...state }
}
default: {
return {...state }
}
};
如果我从某个操作中执行了以下操作:
dispatch({type: ONE});
dispatch({type: TWO});
dispatch({type: THREE});
他们能保证按照那个顺序解决吗?意思是,状态是同步的还是“单线程的”?或者 case TWO: {}
,例如,在 case ONE: {}
完成之前触发?
dispatch
默认情况下是同步的,因此,只要您在动作创建者中没有异步调用,它们就会按照声明的顺序执行。
在redux's docs中暗示dispatch()
是同步的:
Action creators can also be asynchronous and have side-effects. You
can read about async actions in the advanced tutorial to learn how to
handle AJAX responses and compose action creators into async control
flow.
我正在尝试寻找有关 redux reducer 的信息,但我认为术语可能有所不同,因为这是 JavaScript。
假设以下减速器:
import {
ONE,
TWO,
THREE
} from "../actions/types";
export default function reducer(state = { /* some init state here */ }, action) {
switch (action.type) {
case ONE: {
// this case takes 500ms to finish
return {...state }
}
case TWO: {
// this case takes 200ms to finish
return {...state }
}
case THREE: {
// this case takes 100ms to finish
return {...state }
}
default: {
return {...state }
}
};
如果我从某个操作中执行了以下操作:
dispatch({type: ONE});
dispatch({type: TWO});
dispatch({type: THREE});
他们能保证按照那个顺序解决吗?意思是,状态是同步的还是“单线程的”?或者 case TWO: {}
,例如,在 case ONE: {}
完成之前触发?
dispatch
默认情况下是同步的,因此,只要您在动作创建者中没有异步调用,它们就会按照声明的顺序执行。
在redux's docs中暗示dispatch()
是同步的:
Action creators can also be asynchronous and have side-effects. You can read about async actions in the advanced tutorial to learn how to handle AJAX responses and compose action creators into async control flow.