-1
\$\begingroup\$

I am passing a in as a parameter and want to return b.

This works, however I feel like it is wrong because I shouldn't have to pass a in at the return. Also, if I don't include the type for a, I get an error that says Parameter 'path' implicitly has an 'any' type.

async function exampleFunc(a: string) {
    const b = async (a: any) => {
        try {
            // blah blah 
        } catch (error) {
            console.log(error);
        }
    };
    return b(a);
}

How can I improve this?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to the Code Review site where we review actual working code from your project and provide suggestions on how to improve the code. The code in the question is very hypothetical and doesn't appear to be real code from your project. Please see our guidelines. \$\endgroup\$ – pacmaninbw Sep 8 '20 at 23:52
  • \$\begingroup\$ There is nothing wrong in tail calls. \$\endgroup\$ – greybeard Sep 10 '20 at 6:04
1
\$\begingroup\$

You declared a function b with an argument declared named a. That argument definition "hides" or "overrides" the parent a so you cannot access the parent a when you have this other a declared.

So, you have to pass that argument or it will have an undefined value. If you leave out the argument definition for b entirely or give it a different non-conflicting name, then and only then, can you directly reference the parent argument a.

So, you could do this if you want to directly reference the parent-scoped a from within b without passing it as an argument:

async function exampleFunc(a: string) {
    const b = async () => {
        try {
            console.log(a);
            // blah blah 
        } catch (error) {
            console.log(error);
        }
    };
    return b();
}
\$\endgroup\$
1
  • \$\begingroup\$ Please refrain from answering off topic questions. \$\endgroup\$ – slepic Sep 9 '20 at 5:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.