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 issue with createAsyncThunk #19133

Open
mvarakin opened this issue Jul 12, 2022 · 3 comments · May be fixed by #22452
Open

async/await issue with createAsyncThunk #19133

mvarakin opened this issue Jul 12, 2022 · 3 comments · May be fixed by #22452
Labels
area: cleanup $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ theme: react $100 https://www.jhipster.tech/bug-bounties/

Comments

@mvarakin
Copy link

mvarakin commented Jul 12, 2022

Shouldn't axios.get... calls use await prefix?

Currently I have typescript linter errors over all the reducers in getEntities and getEntity
"Async arrow function has no 'await' expression."

JHipster version used to create the project is 7.8.1

export const getEntities = createAsyncThunk('employee/fetch_entity_list', async ({ page, size, sort }: IQueryParams) => {
  const requestUrl = `${apiUrl}?cacheBuster=${new Date().getTime()}`;
  return axios.get<IEmployee[]>(requestUrl);
});

export const getEntity = createAsyncThunk(
  'employee/fetch_entity',
  async (id: string | number) => {
    const requestUrl = `${apiUrl}/${id}`;
    return axios.get<IEmployee>(requestUrl);
  },
  { serializeError: serializeAxiosError }
);

@mshima
Copy link
Member

mshima commented Jul 12, 2022

Please remove async from the arrow function.
If there is no await, async is not required.
By returning a Promise (async function call) the method is implicitly async.

Can you provide a PR?

@mshima
Copy link
Member

mshima commented Jul 12, 2022

export const getEntities = createAsyncThunk('<%= entityInstance %>/fetch_entity_list', async ({ page, size, sort }: IQueryParams) => {

We can remove others functions warnings by changing.

async (entity: I<%= entityReactName %>, thunkAPI) => {
<%_ if (!paginationInfiniteScroll) { _%>
const result = await axios.post<I<%= entityReactName %>>(apiUrl, cleanEntity(entity));
thunkAPI.dispatch(getEntities({}));
return result;
<%_ } else { _%>
return axios.post<I<%= entityReactName %>>(apiUrl, cleanEntity(entity));
<%_ } _%>

To

   async (entity: I<%= entityReactName %>, thunkAPI) => {
      const result = await axios.post<I<%= entityReactName %>>(apiUrl, cleanEntity(entity));
<%_ if (!paginationInfiniteScroll) { _%>
     thunkAPI.dispatch(getEntities({})); 
<%_ } _%> 
     return result;

@deepu105 deepu105 added $100 https://www.jhipster.tech/bug-bounties/ $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ labels Jan 9, 2023
@hide212131
Copy link
Contributor

I'm considering submitting a PR for this issue, if that's okay?
The solution has been almost entirely pointed out by @mshima, so it's not really my contribution, but it has been left unaddressed for a while.

(Creating a test case would require a somewhat forced approach such as checking if the action's content is of the Promise type, so I don't plan to do so.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: cleanup $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ theme: react $100 https://www.jhipster.tech/bug-bounties/
Projects
None yet
4 participants