{"id":212,"date":"2019-12-27T03:08:00","date_gmt":"2019-12-27T03:08:00","guid":{"rendered":""},"modified":"2024-09-13T15:04:52","modified_gmt":"2024-09-13T15:04:52","slug":"javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises","status":"publish","type":"post","link":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/","title":{"rendered":"JavaScript How To Convert Recursive Function With Callbacks to Use Promises"},"content":{"rendered":"<p>I updated one of my websites to use JavaScript promises instead of callbacks. It was pretty straightforward, but got a little tricky for a recursive function. Below is how to do the conversion update:<\/p>\n<p>Recursive callback function:<\/p>\n<pre><\/pre>\n<p>\nfunction doSomething(value) {<br \/>\n&nbsp; &nbsp; function callback(result) {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(&#8216;Done &#8216; + result);<br \/>\n&nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; theRecursiveFunction(value, callback);&nbsp; &nbsp;<br \/>\n}<\/p>\n<p>function theRecursiveFunction(value, next) {&nbsp; <br \/>\n&nbsp; &nbsp; if (value&gt; 3) {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(&#8216;Try again &#8216; + value);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; theRecursiveFunction(value-1, next);<br \/>\n&nbsp; &nbsp; } else {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; next(value);<br \/>\n&nbsp; &nbsp; }<br \/>\n}<\/p>\n<p>doSomething(5);<\/p>\n<p>\nRecursive function with promises:<\/p>\n<pre><\/pre>\n<p>\nfunction doSomething(value) {<br \/>\n&nbsp; &nbsp; theRecursiveFunction(value)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; .then((result) =&gt; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(&#8216;Done &#8216; + result);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>\n}<\/p>\n<p>function theRecursiveFunction(value) {<br \/>\n&nbsp; &nbsp; return new Promise((resolve, reject) =&gt; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; if (value &gt; 3) {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"white-space: pre;\"> <\/span>&nbsp; &nbsp;console.log(&#8216;Try again &#8216; + value);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; theRecursiveFunction(value-1)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .then((result) =&gt; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resolve(result);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; } else {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resolve(value);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; });<br \/>\n}<\/p>\n<p>doSomething(5);<\/p>\n<p>\n(paid links)<\/p>\n<p><a href=\"https:\/\/amzn.to\/2WjqvDh\" rel=\"noopener\" target=\"_blank\">Learn JavaScript<\/a><\/p>\n<p><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=B00J9TMSDU&amp;asins=B00J9TMSDU&amp;linkId=608a143740bc3663981ba41e1e48e3a2&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1949815005&amp;asins=1949815005&amp;linkId=7a0355a9f01f9d433c67d18cc58a8a68&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1119366445&amp;asins=1119366445&amp;linkId=ef46f27fdbd3cff5e6c8aa67c463a1b9&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><\/p>\n<div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I updated one of my websites to use JavaScript promises instead of callbacks. It was pretty straightforward, but got a little tricky for a recursive function. Below is how to do the conversion update: Recursive callback function: function doSomething(value) { &nbsp; &nbsp; function callback(result) { &nbsp; &nbsp; &nbsp; &nbsp; console.log(&#8216;Done &#8216; + result); &nbsp; &nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[340,342],"tags":[],"class_list":["post-212","post","type-post","status-publish","format-standard","hentry","category-javascript","category-promises"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript How To Convert Recursive Function With Callbacks to Use Promises - Zofxare Blog Home<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript How To Convert Recursive Function With Callbacks to Use Promises - Zofxare Blog Home\" \/>\n<meta property=\"og:description\" content=\"I updated one of my websites to use JavaScript promises instead of callbacks. It was pretty straightforward, but got a little tricky for a recursive function. Below is how to do the conversion update: Recursive callback function: function doSomething(value) { &nbsp; &nbsp; function callback(result) { &nbsp; &nbsp; &nbsp; &nbsp; console.log(&#8216;Done &#8216; + result); &nbsp; &nbsp; [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/\" \/>\n<meta property=\"og:site_name\" content=\"Zofxare Blog Home\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-27T03:08:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-13T15:04:52+00:00\" \/>\n<meta name=\"author\" content=\"adminuser\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"adminuser\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\\\/\"},\"author\":{\"name\":\"adminuser\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"headline\":\"JavaScript How To Convert Recursive Function With Callbacks to Use Promises\",\"datePublished\":\"2019-12-27T03:08:00+00:00\",\"dateModified\":\"2024-09-13T15:04:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\\\/\"},\"wordCount\":265,\"commentCount\":0,\"articleSection\":[\"javascript\",\"promises\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\\\/\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\\\/\",\"name\":\"JavaScript How To Convert Recursive Function With Callbacks to Use Promises - Zofxare Blog Home\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\"},\"datePublished\":\"2019-12-27T03:08:00+00:00\",\"dateModified\":\"2024-09-13T15:04:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/27\\\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript How To Convert Recursive Function With Callbacks to Use Promises\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\",\"name\":\"Zofxare Blog Home\",\"description\":\"The Zofxare blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\",\"name\":\"adminuser\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"caption\":\"adminuser\"},\"sameAs\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\"],\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/author\\\/adminuser\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript How To Convert Recursive Function With Callbacks to Use Promises - Zofxare Blog Home","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript How To Convert Recursive Function With Callbacks to Use Promises - Zofxare Blog Home","og_description":"I updated one of my websites to use JavaScript promises instead of callbacks. It was pretty straightforward, but got a little tricky for a recursive function. Below is how to do the conversion update: Recursive callback function: function doSomething(value) { &nbsp; &nbsp; function callback(result) { &nbsp; &nbsp; &nbsp; &nbsp; console.log(&#8216;Done &#8216; + result); &nbsp; &nbsp; [&hellip;]","og_url":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/","og_site_name":"Zofxare Blog Home","article_published_time":"2019-12-27T03:08:00+00:00","article_modified_time":"2024-09-13T15:04:52+00:00","author":"adminuser","twitter_card":"summary_large_image","twitter_misc":{"Written by":"adminuser","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/#article","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/"},"author":{"name":"adminuser","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"headline":"JavaScript How To Convert Recursive Function With Callbacks to Use Promises","datePublished":"2019-12-27T03:08:00+00:00","dateModified":"2024-09-13T15:04:52+00:00","mainEntityOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/"},"wordCount":265,"commentCount":0,"articleSection":["javascript","promises"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/","url":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/","name":"JavaScript How To Convert Recursive Function With Callbacks to Use Promises - Zofxare Blog Home","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website"},"datePublished":"2019-12-27T03:08:00+00:00","dateModified":"2024-09-13T15:04:52+00:00","author":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"breadcrumb":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-how-to-convert-recursive-function-with-callbacks-to-use-promises\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zofxare.com\/zofxare\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript How To Convert Recursive Function With Callbacks to Use Promises"}]},{"@type":"WebSite","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website","url":"https:\/\/zofxare.com\/zofxare\/blog\/","name":"Zofxare Blog Home","description":"The Zofxare blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zofxare.com\/zofxare\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8","name":"adminuser","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","caption":"adminuser"},"sameAs":["https:\/\/zofxare.com\/zofxare\/blog"],"url":"https:\/\/zofxare.com\/zofxare\/blog\/author\/adminuser\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/comments?post=212"}],"version-history":[{"count":1,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/212\/revisions"}],"predecessor-version":[{"id":245,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/212\/revisions\/245"}],"wp:attachment":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/categories?post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/tags?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}