-1

I recently read a code like this one in a react project:

export const funcName = (
  arg1: Type1,
  arg2: Type2,
  ...
  argN: TypeN,
) => async (dispatch: Dispatch) => {
 // function body
 // ..
};

and I can't understand the double arrow (() => async => {}) syntax.

Can some tell me what is happening here? Thanks

1
  • 2
    You're going to find TypeScript and React tricky if you're not familiar with basic JS. Commented Nov 9, 2023 at 19:16

2 Answers 2

3

What this is doing is saying you're returning a function that takes Dispatch. Most likely this is used for Redux in some way.

This is also called "higher order functions" which are functions that take in some arguments and return a function. In this case, an async function.

To be more clear, the function inside does not get executed when you call funcname, it just gets built. The async function has access to the args passed to funcname though, so it helps build it.

Sign up to request clarification or add additional context in comments.

Comments

1

If you convert that to function syntax it may be more understandable:

export function funcName (
  arg1: Type1,
  arg2: Type2,
  ...
  argN: TypeN,
) {
  return async function takesDispatch(dispatch: Dispatch) {
   // function body
   // ..
  }
}

So it gets called as

let takesDispatch = funcName(arg1, arg2, ... args)
await takesDispatch(dispatch)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.