gpt4 book ai didi

reactjs - 保证发货顺序

转载 作者:行者123 更新时间:2023-12-04 22:14:01 25 4
gpt4 key购买 nike

我试图找出有关 redux reducer 的一些信息,但我认为术语可能会有所不同,因为这是 JavaScript。

假设有以下 reducer :

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 }
}
};

如果我从一个 Action 中执行以下操作:
dispatch({type: ONE});
dispatch({type: TWO});
dispatch({type: THREE});

他们保证按那个顺序解决吗?意思是,状态是同步的还是“单线程的”?或者会 case TWO: {}例如,在 case ONE: {} 之前触发完全的?

最佳答案

dispatch默认情况下是同步的,因此,只要您在 Action 创建者中没有异步调用,它们就会按照声明的顺序执行。

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.

关于reactjs - 保证发货顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43188378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com