{"id":432,"date":"2022-08-30T15:01:15","date_gmt":"2022-08-30T15:01:15","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/pass-optional-function-as-parameter-to-another-function-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:15","modified_gmt":"2022-08-30T15:01:15","slug":"pass-optional-function-as-parameter-to-another-function-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/pass-optional-function-as-parameter-to-another-function-collection-of-common-programming-errors\/","title":{"rendered":"pass optional function as parameter to another function-Collection of common programming errors"},"content":{"rendered":"<p>Options of how I want to call a function:<\/p>\n<pre><code>myAjax(\"http:\/\/ajaxurl.com\", { prop: val }, function(resp) { alert(resp); });\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>function handleSuccess(resp) { alert(resp); }\nmyAjax(\"http:\/\/ajaxurl.com\", { prop: val }, handleSuccess);\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>myAjax(\"http:\/\/ajaxurl.com\", { prop: val }, null);\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>myAjax(\"http:\/\/ajaxurl.com\", { prop: val }); \/\/ param not even provided\n<\/code><\/pre>\n<p>How can I handle this on the myAjax function definition? Something like this&#8230;?<\/p>\n<pre><code>function myAjax(url, jsonData, successFunc) {\n  $.ajax({\n    ... all the necessary ajax stuff that normally goes here\n    , success: function(response) {\n      \/\/ custom myAjax success handling goes here. this must happen before optionally-passed-in successFunc gets executed\n\n      \/\/ what goes here? this?\n      if (typeof successFunc != 'undefined' &amp;&amp; successFunc != null) {\n        successFunc(response);\n      }\n    }\n  });\n}\n<\/code><\/pre>\n<p>I tried something like the above, but, it did not call the success function. Do I need to check if successFunc is a function?<\/p>\n<p>Thanks!<\/p>\n<ol>\n<li>\n<p>Instead of testing against unknown types, verify that the function is indeed a function:<\/p>\n<pre><code>if (typeof successFunc == 'function') {\n    successFunc(response);\n}\n<\/code><\/pre>\n<p>Your current code does not prevent <code>successFunc<\/code> from being run, though. Make sure that the AJAX request is successfully handled (no errors no cross-domain restrictions).<\/p>\n<p>Since your code doesn&#8217;t event reach the point of calling <code>successFunc<\/code>, it&#8217;s likely that an error is generated before <code>if (typeof ...<\/code>.<\/p>\n<\/li>\n<li>\n<p>Testing that the typeof successFunc is &#8220;function&#8221; will do the trick:<\/p>\n<pre><code>function myAjax(url, jsonData, successFunc) {\n  $.ajax({\n    ... all the necessary ajax stuff that normally goes here\n    , success: function(response) {\n      \/\/ custom myAjax success handling goes here. this must happen before optionally-passed-in successFunc gets executed\n\n      \/\/ what goes here? this?\n      if (typeof successFunc === 'function') {\n        successFunc(response);\n      }\n    }\n  });\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:42:11. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Options of how I want to call a function: myAjax(&#8220;http:\/\/ajaxurl.com&#8221;, { prop: val }, function(resp) { alert(resp); }); or function handleSuccess(resp) { alert(resp); } myAjax(&#8220;http:\/\/ajaxurl.com&#8221;, { prop: val }, handleSuccess); or myAjax(&#8220;http:\/\/ajaxurl.com&#8221;, { prop: val }, null); or myAjax(&#8220;http:\/\/ajaxurl.com&#8221;, { prop: val }); \/\/ param not even provided How can I handle this on the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-432","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=432"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/432\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}