Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async / Await - Redux Thunk with a returned value #7780

Closed
oli-laban opened this issue Jun 3, 2019 · 4 comments
Closed

Async / Await - Redux Thunk with a returned value #7780

oli-laban opened this issue Jun 3, 2019 · 4 comments
Labels

Comments

@oli-laban
Copy link

Flow version: ^0.92.0

Expected behavior

No errors.

Actual behavior

Cannot call await with `checkAuthStatus()` bound to `p` because `ThunkAction` [1] is incompatible with `Promise` [2].


I'm trying to Flow type a Redux Thunk action that has a return value. I think this is related to #5294 in that I'm getting the same useless error as some of the comments although none of the workarounds posted are working for me.

I'm using the Redux Thunk example from the Flow docs (I've tried adding an explicit return type of Promise to the ThunkAction and async inner function):

type PromiseAction = Promise<Action>;
type ThunkAction = (dispatch: Dispatch, getState: GetState) => any;
type Dispatch = (action: Action | ThunkAction | PromiseAction | Array<Action>) => any;

export const checkAuthStatus = (): ThunkAction => async (dispatch) => {
  const loggedIn: boolean = false;
  const promptSkipped: boolean = true;

  dispatch(setLoggedIn(loggedIn));

  return promptSkipped;
};

This works if I simply call await checkAuthStatus(), but if I try to assign the returned value (const promptSkipped = await checkAuthStatus()), I get the above error.

Try-Flow Example


I'm brand-new to Flow so hopefully there isn't a glaringly obvious problem with the way I'm trying to do this. StackOverflow question here.

@goodmind
Copy link
Contributor

goodmind commented Jun 4, 2019

You're returning function, not promise

@oli-laban
Copy link
Author

@goodmind I'm returning a function that returns a Promise, yes.

@oli-laban
Copy link
Author

@jcready Thanks, how would this work in a component context where the redux-thunk middleware handles the calling of the inner function? e.g.

async componentDidMount() => {
  const promptSkipped = await this.props.authStatus();
  ...
}

...

const mapDispatchToProps = (dispatch: Dispatch) => {
  authStatus: dispatch(checkAuthStatus()),
};

export default connect(null, mapDispatchToProps)(TheComponent)

@SamChou19815 SamChou19815 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants