1

I am trying to understand this arrow function declaration but I can't. Someone can explain what is getFooOfIdImpl in this funtion and why there's several async declarations? Thank you

const getFooOfId = (getFooOfIdImpl = async (fooId) => { throw new Error(`Can't retrieved foo of id ${fooId} : missing implementation`) }) => async fooId => {
 try {
    const fooData = await getFooOfIdImpl(fooId);
    return FooData(fooData);
  } catch (err) {
    throw new Error(`Unable to retrieve Foo with id ${fooId}`);
  }
}
Estefania
  • 19
  • 2
  • There is no `getFooOfImpl` in the code, do you mean `getFooOfIdImpl`? (And if so, which one...) – DBS Jan 11 '22 at 11:58
  • 1
    If you call `getFooOfId` without passing a value for the `getFooOfIdImpl` parameter, `getFooOfIdImpl` will be assigned a default value, i.e. `async (fooId) => { throw new Error(\`Can't retrieved foo of id ${fooId} : missing implementation\`) }` – Yousaf Jan 11 '22 at 12:00
  • 1
    It's just higher-order functions - `getFooOfId` takes a function as a parameter and returns a function - with a default parameter value. – jonrsharpe Jan 11 '22 at 12:03
  • 1
    Here a a slightly rewritten version as [jsfiddle](https://jsfiddle.net/1causfjp/) that should make it easier to understand what this pice of code does. – t.niese Jan 11 '22 at 12:13
  • I meant getFooOfIdImpl, I updated it – Estefania Jan 12 '22 at 09:31
  • Great, now I understand it! thanks everybody for your anwsers and thank you @t.niese for the code – Estefania Jan 12 '22 at 09:52

0 Answers0