introductionToAppsScript
introductionToAppsScript copied to clipboard
TypeError in superFunction()
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 ?
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
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".