introductionToAppsScript icon indicating copy to clipboard operation
introductionToAppsScript copied to clipboard

TypeError in superFunction()

Open ygautomo opened this issue 3 years ago • 2 comments

Hi Ben.

I've tried this command

// pass functions as arguments to other functions
const superFunction = (fn, a, b) => fn(a,b);

console.log(superFunction(multFunction,2,5));

I'm selecting superFunction to run, and I found an error: TypeError: fn is not a function

Do you have explanation about this ?

ygautomo avatar Aug 28 '21 08:08 ygautomo

Hi,

Looks like you're seeing this error because you're running the superFunction function at execution time:

[image: callbackError.jpg]

When this runs superFunc the "fn" is not defined anywhere so it throws the error.

If you select a different function that is defined e.g. multFunction and run then you should get the answer and no error message, because now "fn" is defined because it gets passed it at runtime. The console.log always gets executed because it's in the global space outside of any function definition.

[image: callback.jpg]

Hope this helps!

Cheers, Ben

On Sat, Aug 28, 2021 at 4:23 AM ygautomo @.***> wrote:

Hi Ben.

I've tried this command

// pass functions as arguments to other functions const superFunction = (fn, a, b) => fn(a,b);

console.log(superFunction(multFunction,2,5));

But I found an error: TypeError: fn is not a function

Do you have explanation about this ?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/benlcollins/introductionToAppsScript/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACRT2QIJ5723UD277OJRG6DT7CMHRANCNFSM5C64HUJQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

-- Ben Collins benlcollins.com

Check out my free Google Sheets online training course: https://courses.benlcollins.com/p/advanced30

benlcollins avatar Aug 31 '21 13:08 benlcollins

Ben, thanks for answering this, because I ran into the same issue as I followed along typing and executing the code. I was so used to "Save", change to the function that I just wrote, and "Run" from the previous tutorials that I didn't notice that you hadn't changed to the "superFunction" before hitting "Run".

mowestusa avatar Jun 02 '22 20:06 mowestusa