0

Why does my editor (PHPStorm) say that I'm not gonna need 'await'? It works normally...
Here's the code:

export const fetchBoards = (): AppThunk =>
  async function (dispatch): Promise<void> {
    try {
      const { boards }: { boards: IBoardItem[] } = await api.get('/board');
      dispatch({ type: 'UPDATE_BOARDS', payload: boards });
      window.console.log('Success');
    } catch (e) {
      window.console.error(e);
      dispatch({ type: 'ERROR', payload: e });
    }
  };
export const addBoard = (board: { title: string }): AppThunk =>
  async function (dispatch): Promise<void> {
    try {
      const res = await api.post('/board', board);
      window.console.log(res);

      window.console.log('Before');
      await dispatch(fetchBoards()); // 'await' has no effect on the type of this expression
      window.console.log('After');
    } catch (e) {
      window.console.error(e);
      const res = dispatch({ type: 'ERROR', payload: e });
    }
  };
  • 5
    if `dispatch` doesn't return a promise, `await` accomplishes very little. – CollinD Feb 01 '23 at 17:59
  • Also the functions returned by `fetchBoards()` and `addBoard()` do not return anything. – Pointy Feb 01 '23 at 18:00
  • @CollinD `dispatch` in this case does return a promise though - the types for dispatch itself are just wrong. – phry Feb 02 '23 at 07:52
  • @Pointy they are `async`, so they return a `Promise` implicitly. – phry Feb 02 '23 at 07:52
  • @phry yes but there's never going to be a value to wait for. I guess that's the intention. – Pointy Feb 02 '23 at 12:28
  • @Pointy I assume they want to wait until the api request is finished before kicking off a new request - independently of the return value. – phry Feb 02 '23 at 12:31

1 Answers1

3

Because your dispatch is typed incorrectly and not aware of the redux-thunk middleware you are using.

Please set up your project up according to the TypeScript quick start https://redux-toolkit.js.org/tutorials/typescript that will give you a correct AppDispatch type to use for your AppThunk type.

Please also note that you are generally writing a very outdated (pre-2019) style of Redux - in modern Redux you would not dispatch "ACTION_TYPES" with manual objects and it is generally 1/4 of the code. Please read why Redux Toolkit is how to use Redux today.

phry
  • 35,762
  • 5
  • 67
  • 81