{"id":1898,"date":"2014-12-12T13:15:23","date_gmt":"2014-12-12T11:15:23","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=1898"},"modified":"2014-12-10T14:55:15","modified_gmt":"2014-12-10T12:55:15","slug":"promises-vs-callbacks-code-comparison","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/","title":{"rendered":"Promises vs Callbacks \u2013 Code comparison"},"content":{"rendered":"<p>I am not going to highlight pros of promises and cons of callbacks. There is plenty of good reading about this topic out there. I was recently writing simple Node module and decided to learn promises during its implementation. The result\u00a0 can be interesting as comparison of Promises vs Callbacks approach applied to the same problems, because project contains<\/p>\n<ul>\n<li>Two implementations of same module\n<ul>\n<li><a href=\"https:\/\/github.com\/lkrnac\/jasstor\/blob\/blog-2014-10-26-promises-vs-callbacks-comparion\/lib\/jasstor.js\">Promises based code<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/lkrnac\/jasstor\/blob\/master\/lib\/jasstorClbk.js\">Callbacks based code<\/a><\/li>\n<\/ul>\n<\/li>\n<li>Two unit tests for the same module\n<ul>\n<li><a href=\"https:\/\/github.com\/lkrnac\/jasstor\/blob\/blog-2014-10-26-promises-vs-callbacks-comparion\/test\/jasstorPromiseSpec.js\">Promises based test<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/lkrnac\/jasstor\/blob\/blog-2014-10-26-promises-vs-callbacks-comparion\/test\/jasstorClbkSpec.js\">Callbacks based test<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>These are glued with Gulp build system to execute tests with all possible combinations. So for example Callbacks based test is executed also against Promises based module.<\/p>\n<p>I named the project <strong>Jasstor<\/strong>.<a href=\"https:\/\/github.com\/lkrnac\/jasstor\/tree\/blog-2014-10-26-promises-vs-callbacks-comparion#getting-started\"> I am planning to use it for storage credentials into JSON file, hashing and verification of credentials<\/a>. It stores user name, hashed password and user\u2019s role in string format. <a href=\"https:\/\/github.com\/lkrnac\/jasstor\/tree\/blog-2014-10-26-promises-vs-callbacks-comparion\">Here is Github branch dedicated to this blog post<\/a>, so that it\u2019ll stay consistent. Please bear in mind, that I am learning Node development, ES6 features, Promises and Gulp on this project. So I could easily miss handy tricks or misused some features.<\/p>\n<p>Project uses these main technologies:<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/google\/traceur-compiler\">Traceur<\/a> \u2013 for ES6 transpilation<\/li>\n<li><a href=\"https:\/\/github.com\/petkaantonov\/bluebird\">Bluebird<\/a> \u2013 promises library<\/li>\n<li><a href=\"http:\/\/mochajs.org\/\">Mocha<\/a> and <a href=\"https:\/\/github.com\/shouldjs\/should.js\">Should<\/a> for unit testing<\/li>\n<li><a href=\"https:\/\/github.com\/ncb000gt\/node.bcrypt.js\">node.bcrypt.js <\/a>&#8211; for hashing passwords<\/li>\n<\/ul>\n<p>I decided to have these constraints for Promises<\/p>\n<ul>\n<li>Mocha will be excluded from promisification, so <strong><code>describe<\/code><\/strong> and <strong><code>it<\/code><\/strong> will be used with callbacks.<\/li>\n<li><strong>Jasstor<\/strong>\u2018s API will follow standard Node JS patterns\n<ul>\n<li>All functions are asynchronous with callback as last parameter<\/li>\n<li>First parameter of callback is always error<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>When function signatures follow Node JS patters, it allows for <a href=\"https:\/\/github.com\/petkaantonov\/bluebird\/blob\/master\/API.md#promisification\">promisification of modules<\/a>. Such modules can be integrated into promise chain easily. But at the same time API isn\u2019t tied to Promises at all. I like this approach because both camps (Promises or Callbacks fans) are happy.<\/p>\n<p>Let\u2019s take a look at code. I will explain and compare only most verbose use case and leave the rest for curios readers. <a href=\"https:\/\/github.com\/lkrnac\/jasstor\/tree\/blog-2014-10-26-promises-vs-callbacks-comparion\">You can find the code here<\/a>.<\/p>\n<h2>Callback vs Promises \u2013 Tests comparison<\/h2>\n<p>Enough talking, let\u2019s take a look at code. I\u2019ll start with tests explanation as it promotes TDD thinking. Use case should test if existing password is overwritten when credentials for same user are stored. There are these phases in the test:<\/p>\n<ol>\n<li>Credentials file with initial password is created<\/li>\n<li>Read initial password<\/li>\n<li>Overwrite initial password with different one<\/li>\n<li>Read new password<\/li>\n<li>Verify that new password is different to initial one<\/li>\n<\/ol>\n<h2><a href=\"https:\/\/github.com\/lkrnac\/jasstor\/blob\/blog-2014-10-26-promises-vs-callbacks-comparion\/test\/jasstorClbkSpec.js\">Callbacks based test<\/a><\/h2>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var credentialsFile = 'testCredentials.txt';\r\n\r\nvar checkError = function(err, done) {\r\n  if (err) {\r\n    done(err);\r\n  }\r\n};\r\n\r\nvar readPassword = (credentialsFile, done, callback) =&gt; {\r\n  fs.readFile(credentialsFile, (err, data) =&gt; {\r\n    checkError(err, done);\r\n    var jsonData = JSON.parse(data);\r\n    callback(jsonData.user.password);\r\n  });\r\n};\r\ndescribe('jasstor', () =&gt; {\r\n  var jasstor = new Jasstor(credentialsFile);\r\n\r\n  describe('when creadentials file already exist', () =&gt; {\r\n    beforeEach(done =&gt; {\r\n      fs.unlink(credentialsFile, () =&gt; {\r\n        jasstor.saveCredentials('user', 'password', 'role', done);\r\n      });\r\n    });\r\n\r\n    it('should overwrite existing password', done =&gt; {\r\n      readPassword(credentialsFile, done, (originalPassword) =&gt; {\r\n        jasstor.saveCredentials('user', 'password1', 'role', err =&gt; {\r\n          checkError(err, done);\r\n          readPassword(credentialsFile, done, (newPassword) =&gt; {\r\n            newPassword.should.be.ok;\r\n            originalPassword.should.be.ok;\r\n            newPassword.should.not.equal(originalPassword);\r\n            done();\r\n          });\r\n        });\r\n      });\r\n    });\r\n  });\r\n});<\/pre>\n<p><code><strong>jasstor<\/strong><\/code> is testing object and <code><strong>jasstor.saveCredentials()<\/strong><\/code> is testing function. There is created helper function <strong><code>readPassword<\/code><\/strong> because password needs to be read twice during test. Pretty straight forward callbacks pyramid. I don\u2019t like calling <strong><code>checkError<\/code><\/strong><code><\/code> at the beginning of each callback. Annoying Node pattern.<\/p>\n<h2><a href=\"https:\/\/github.com\/lkrnac\/jasstor\/blob\/blog-2014-10-26-promises-vs-callbacks-comparion\/test\/jasstorPromiseSpec.js\">Promises based test<\/a><\/h2>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var fs = Bluebird.promisifyAll(require('fs'));\r\nvar credentialsFile = 'testCredentials.txt';\r\n\r\nvar readPassword = (credentialsFile, userName, done) =&gt; {\r\n  return fs.readFileAsync(credentialsFile)\r\n    .then(JSON.parse)\r\n    .then(jsonData =&gt; {\r\n      return jsonData&#x5B;userName].password;\r\n    }).catch(done);\r\n};\r\n\r\nvar ignoreCallback = () =&gt; {};\r\n\r\ndescribe('jasstor tested with promises', () =&gt; {\r\n  var jasstor = Bluebird.promisifyAll(new Jasstor(credentialsFile));\r\n\r\n  describe('when creadentials file already exist', () =&gt; {\r\n    beforeEach(done =&gt; {\r\n      fs.unlinkAsync(credentialsFile)\r\n        .finally(() =&gt; {\r\n          jasstor.saveCredentials('user', 'password', 'role', done);\r\n        }).catch(ignoreCallback);\r\n    });\r\n\r\n    it('should overwrite existing password', done =&gt; {\r\n      var originalPassword = readPassword(credentialsFile, 'user', done);\r\n      var newPassword;\r\n      jasstor.saveCredentialsAsync('user', 'password1', 'role')\r\n        .then(() =&gt; {\r\n          newPassword = readPassword(credentialsFile, 'user', done);\r\n          newPassword.should.be.ok;\r\n          originalPassword.should.be.ok;\r\n          newPassword.should.not.equal(originalPassword);\r\n          done();\r\n        }).catch(done);\r\n    });\r\n  });\r\n});<\/pre>\n<p>Important here is <a href=\"https:\/\/github.com\/petkaantonov\/bluebird\/blob\/master\/API.md#promisification\">promisification<\/a> of <code><strong>fs<\/strong><\/code> library (first line). It patches <strong><code>fs<\/code><\/strong> module to have additional methods with <code><strong>Async<\/strong><\/code> suffix. These methods return Promise and doesn\u2019t take callback as parameter. This effectively converts existing API to promise based API. Same is done to testing object <strong><code>jasstor<\/code><\/strong>.<\/p>\n<p>It was slight surprise to me that Promises actually doesn\u2019t enable for less verbose code. Few facts are pretty obvious to me after this comparison:<\/p>\n<ul>\n<li>Much more elegant error handling. As long as error callback has error as first parameter, you can just pass it to <strong><code>catch<\/code><\/strong> block as function reference.<\/li>\n<li>Callbacks pyramid is flattened. This can improve readability. But readability is probably matter of maturity with certain approach.<\/li>\n<\/ul>\n<h2>Callback vs Promises \u2013 Node module comparison<\/h2>\n<p>Now I am going to compare code that was tested by tests above.<\/p>\n<h2><a href=\"https:\/\/github.com\/lkrnac\/jasstor\/blob\/master\/lib\/jasstorClbk.js\">Callbacks based code<\/a><\/h2>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var hashPassword = (password, callback) =&gt; {\r\n  bcrypt.genSalt(10, (err, salt) =&gt; bcrypt.hash(password, salt, callback));\r\n};\r\n\r\nvar readJsonFile = (storageFile, callback) =&gt; {\r\n  fs.exists(storageFile, (result) =&gt; {\r\n    if (result === false) {\r\n      callback(null, {});\r\n    } else {\r\n      fs.readFile(storageFile, (err, data) =&gt; {\r\n        var jsonData = JSON.parse(data);\r\n        callback(err, jsonData);\r\n      });\r\n    }\r\n  });\r\n};\r\n\r\nmodule.exports = class Jasstor {\r\n  constructor(storageFile) {\r\n    this.storageFile = storageFile;\r\n  }\r\n\r\n  saveCredentials(user, password, role, callback) {\r\n    readJsonFile(this.storageFile, (err, jsonData) =&gt; {\r\n      hashPassword(password, (err, hash) =&gt; {\r\n        jsonData&#x5B;user] = {\r\n          password: hash,\r\n          role: role\r\n        };\r\n        var jsonDataString = JSON.stringify(jsonData);\r\n\r\n        fs.writeFile(this.storageFile, jsonDataString, callback);\r\n      });\r\n    });\r\n  }\r\n};<\/pre>\n<p>Here we have ES6 class <code><strong>Jasstor<\/strong><\/code> with constructor and method that saves credentials into JSON file. There are two helper methods <strong><code>hashPassword<\/code><\/strong> and <code><strong>readJsonFile<\/strong><\/code> to help with repetitive tasks across the <strong><code>Jasstor<\/code><\/strong> class. We can see callback pyramid again. It is slightly simplified by helper functions.<\/p>\n<h2><a href=\"https:\/\/github.com\/lkrnac\/jasstor\/blob\/blog-2014-10-26-promises-vs-callbacks-comparion\/lib\/jasstor.js\">Promises based code<\/a><\/h2>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var fs = Bluebird.promisifyAll(require('fs'));\r\nvar bcrypt = Bluebird.promisifyAll(require('bcrypt'));\r\n\r\nvar hashPassword = password =&gt; {\r\n  return new Promise(resolve =&gt; {\r\n    bcrypt.genSaltAsync(10)\r\n      .then(salt =&gt; {\r\n        return bcrypt.hashAsync(password, salt);\r\n      }).then(resolve);\r\n  });\r\n};\r\n\r\nvar readJsonFile = storageFile =&gt; {\r\n  return new Promise((resolve, reject) =&gt; {\r\n    fs.exists(storageFile, result =&gt; {\r\n      if (result === true) {\r\n        fs.readFileAsync(storageFile)\r\n          .then(JSON.parse)\r\n          .then(resolve)\r\n          .catch(reject);\r\n      } else {\r\n        resolve({});\r\n      }\r\n    });\r\n  });\r\n};\r\n\r\nmodule.exports = class Jasstor {\r\n  constructor(storageFile) {\r\n    this.storageFile = storageFile;\r\n  }\r\n\r\n  saveCredentials(user, password, role, callback) {\r\n    readJsonFile(this.storageFile).then(jsonData =&gt; {\r\n      hashPassword(password).then(hash =&gt; {\r\n        jsonData&#x5B;user] = {\r\n          password: hash,\r\n          role: role\r\n        };\r\n        return jsonData;\r\n      }).then(JSON.stringify)\r\n        .then(jsonDataString =&gt; {\r\n          fs.writeFile(this.storageFile, jsonDataString, callback);\r\n        }).catch(callback);\r\n    }).catch(callback);\r\n  }\r\n};<\/pre>\n<p>Same implementation packed into Promises is more verbose (hopefully I missed some tricks that could made it shorter). I like again simplified\u00a0error handling. You maybe spot\u00a0 that <strong><code>fs.exists<\/code><\/strong> isn\u2019t promisified. <a href=\"http:\/\/devdocs.io\/node\/fs#fs_fs_exists_path_callback\">If you take a look at its API<\/a>, callback doesn\u2019t have error as first parameter. I suspect, this is why <strong><code>fs.existsAsync<\/code><\/strong> doesn\u2019t work correctly. Not sure if this is limitation of Bluebird promises library I am using or Promises A+ specification.<\/p>\n<h2>Conclusion<\/h2>\n<p>Promises are very nice approach that could totally change style of your programming. But I have to admit that I am not 100% sold to it yet. It took me some time to wrap my head around the concept. Promises also seem to me slightly more verbose than callbacks. When you have functions with one parameter and return value, you can nicely chain them with just passing function references into Promise chain. But mostly you don\u2019t have such comfortable APIs and you end up doing \u201cflattened callbacks pyramid\u201d.<\/p>\n<p>I would suggest to try Promises on your project (or small library) and make own opinion. Examples aren\u2019t enough challenging to push the Promises into its limits.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/lkrnac.net\/blog\/2014\/10\/promises-vs-callbacks-comparison\/\">Promises vs Callbacks \u2013 Code comparison<\/a> from our <a href=\"http:\/\/www.webcodegeeks.com\/wcg\/\">WCG partner<\/a> Lubos Krnac at the <a href=\"http:\/\/lkrnac.net\/blog\/\">Lubos Krnac Java blog<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I am not going to highlight pros of promises and cons of callbacks. There is plenty of good reading about this topic out there. I was recently writing simple Node module and decided to learn promises during its implementation. The result\u00a0 can be interesting as comparison of Promises vs Callbacks approach applied to the same &hellip;<\/p>\n","protected":false},"author":19,"featured_media":920,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[65],"class_list":["post-1898","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-promises"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Promises vs Callbacks \u2013 Code comparison - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"I am not going to highlight pros of promises and cons of callbacks. There is plenty of good reading about this topic out there. I was recently writing\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Promises vs Callbacks \u2013 Code comparison - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"I am not going to highlight pros of promises and cons of callbacks. There is plenty of good reading about this topic out there. I was recently writing\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2014-12-12T11:15:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Lubos Krnac\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/luboskrnac\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Lubos Krnac\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/\"},\"author\":{\"name\":\"Lubos Krnac\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/b97949165d80813cf674daef2917541e\"},\"headline\":\"Promises vs Callbacks \u2013 Code comparison\",\"datePublished\":\"2014-12-12T11:15:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/\"},\"wordCount\":1280,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"keywords\":[\"Promises\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/\",\"name\":\"Promises vs Callbacks \u2013 Code comparison - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"datePublished\":\"2014-12-12T11:15:23+00:00\",\"description\":\"I am not going to highlight pros of promises and cons of callbacks. There is plenty of good reading about this topic out there. I was recently writing\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Promises vs Callbacks \u2013 Code comparison\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/b97949165d80813cf674daef2917541e\",\"name\":\"Lubos Krnac\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/de54fe70007a5ff7a805ec6c874812ec129162cb080f66e7a14a05df13da44d1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/de54fe70007a5ff7a805ec6c874812ec129162cb080f66e7a14a05df13da44d1?s=96&d=mm&r=g\",\"caption\":\"Lubos Krnac\"},\"description\":\"Lubos is a Java\/JavaScript developer\/Tech Lead and Automation enthusiast. His religion is to constantly improve his developments skills and learn new approaches. He believes TDD drives better design and nicely decoupled code. Past experience includes C++, Assembler and C#.\",\"sameAs\":[\"http:\/\/lkrnac.net\/blog\/\",\"http:\/\/www.linkedin.com\/pub\/lubos-krnac\/27\/21a\/b2\",\"https:\/\/x.com\/https:\/\/twitter.com\/luboskrnac\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/lubos-krnac\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Promises vs Callbacks \u2013 Code comparison - Web Code Geeks - 2026","description":"I am not going to highlight pros of promises and cons of callbacks. There is plenty of good reading about this topic out there. I was recently writing","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:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/","og_locale":"en_US","og_type":"article","og_title":"Promises vs Callbacks \u2013 Code comparison - Web Code Geeks - 2026","og_description":"I am not going to highlight pros of promises and cons of callbacks. There is plenty of good reading about this topic out there. I was recently writing","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2014-12-12T11:15:23+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","type":"image\/jpeg"}],"author":"Lubos Krnac","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/luboskrnac","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Lubos Krnac","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/"},"author":{"name":"Lubos Krnac","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/b97949165d80813cf674daef2917541e"},"headline":"Promises vs Callbacks \u2013 Code comparison","datePublished":"2014-12-12T11:15:23+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/"},"wordCount":1280,"commentCount":1,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","keywords":["Promises"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/","name":"Promises vs Callbacks \u2013 Code comparison - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","datePublished":"2014-12-12T11:15:23+00:00","description":"I am not going to highlight pros of promises and cons of callbacks. There is plenty of good reading about this topic out there. I was recently writing","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/js-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/promises-vs-callbacks-code-comparison\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"Promises vs Callbacks \u2013 Code comparison"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/b97949165d80813cf674daef2917541e","name":"Lubos Krnac","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/de54fe70007a5ff7a805ec6c874812ec129162cb080f66e7a14a05df13da44d1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/de54fe70007a5ff7a805ec6c874812ec129162cb080f66e7a14a05df13da44d1?s=96&d=mm&r=g","caption":"Lubos Krnac"},"description":"Lubos is a Java\/JavaScript developer\/Tech Lead and Automation enthusiast. His religion is to constantly improve his developments skills and learn new approaches. He believes TDD drives better design and nicely decoupled code. Past experience includes C++, Assembler and C#.","sameAs":["http:\/\/lkrnac.net\/blog\/","http:\/\/www.linkedin.com\/pub\/lubos-krnac\/27\/21a\/b2","https:\/\/x.com\/https:\/\/twitter.com\/luboskrnac"],"url":"https:\/\/www.webcodegeeks.com\/author\/lubos-krnac\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/1898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=1898"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/1898\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/920"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=1898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=1898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=1898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}