redux thunk 函数如何运行?当调用 redux thunk 函数时会发生什么?

How does a redux thunk function operate? What happens when a redux thunk function gets called?

我正在努力思考语法/理解 thunk 函数的流程...
即以下函数:

function incrementAsync() {
  return dispatch => {
    setTimeout(() => {
      dispatch(increment());
    }, 1000);
  };
}

我想我最困惑的是将 dispatch 作为参数部分传入。 incrementAsync 被调用时到底发生了什么?

一个普通的 Redux 动作是一个普通的 JS 对象,如教程中所示:https://redux.js.org/basics/actions

Thunk做的是扩展Redux,让action也可以是函数。当 incrementAsync 被调用时,它 returns 一个接受参数 dispatch 的新函数。然后 Redux(通过 Thunk)调用该函数,将 Redux 的 dispatch() 函数作为参数传入,使您能够 (a) 同步分派更多操作。

这一切都是利用了 JS 函数是 first-class 语言成员并且可以分配给变量并传递的事实。