{"id":449,"date":"2015-08-21T05:27:01","date_gmt":"2015-08-21T05:27:01","guid":{"rendered":"http:\/\/shabeebk.com\/blog\/?p=449"},"modified":"2022-03-30T06:45:00","modified_gmt":"2022-03-30T06:45:00","slug":"promises-and-call-back","status":"publish","type":"post","link":"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/","title":{"rendered":"promises and call back"},"content":{"rendered":"<p>promises and call back are only doing similar job,&nbsp; but sometimes we have to think whether to have a callback function or promises.<\/p>\n<p class=\"\">Here we will be discussing about promises and call back function. So let have look on both callback function and promises and how it works.<\/p>\n<h4><strong>Call back function<\/strong><\/h4>\n<p>Callback functions are functions that are executed after completing one function.<\/p>\n<p>For example, if we have to do something after getting the result from the server, then we can write a call back function and it will execute once it gets the result from the server.<\/p>\n<p>Let us have a look on the below example:<\/p>\n<pre class=\"lang:default decode:true\" title=\"callback function\">function first(arg1, callback){\n\/\/do some code\n\/\/after that call back funtion will be callled\nif(type of callback ==function ) \ncallback(options);\n\n}<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong>promises<\/strong><\/h4>\n<p class=\"\">Promise is just a way of writing asynchronous code in a more synchronous fashion.<br \/>\nie, When you call promises, it will return a promise value and the calling code will wait until the promise is full filled, before executing next.<\/p>\n<p class=\"\">angularjs promises example:<\/p>\n<pre class=\"lang:default decode:true\" title=\"angularjs-promises\">var promise = asyncGreet('Robin Hood');\npromise.then(function(greeting) {\nalert('Success: ' + greeting);\n}, function(reason) {\nalert('Failed: ' + reason);\n});\n \/\/then(successCallback, errorCallback, notifyCallback)\n\nWe can pass three parameters for \".then\" function, of which one is on success, \nanother one on failure and last one is notifyCallback .notifyCallback can be \ncalled more than one time to the show progress and it is an optional parameter.<\/pre>\n<h4>&nbsp;Some differences between call back promises<\/h4>\n<ul>\n<li>Call back can be called once or more than once (depends on how it is written).<\/li>\n<li>Promises will be called only once<\/li>\n<li>Call back can throw errors and exceptions (have to write more call back for errors)<\/li>\n<li>Promises have error handling<\/li>\n<li>\n<pre class=\"lang:default decode:true\">then(fulfilledHandler, errorHandler, progressHandler)<\/pre>\n<\/li>\n<li>No states in call back<\/li>\n<li>May have one of 3 states: unfulfilled, fulfilled and failed or only move from unfulfilled to either fulfilled or failed<\/li>\n<li>In call back we can call N function in N levels<\/li>\n<li>Can write a sequence of N async calls without N levels of indentation<\/li>\n<\/ul>\n<p>These are some of my view points, if you have any suggestions or addons, you comments are welcome.<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"449\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;226,869&nbsp;total views, &nbsp;9&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>promises and call back are only doing similar job,&nbsp; but sometimes we have to think whether to have a callback function or promises. Here we will be discussing about promises and call back function. So let have look on both callback function and promises and how it works. Call back function Callback functions are functions [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"449\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;226,869&nbsp;total views, &nbsp;9&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[4,1],"tags":[77,78],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v18.4.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>promises and call back - shabeeb Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"promises and call back - shabeeb Blog\" \/>\n<meta property=\"og:description\" content=\"promises and call back are only doing similar job,\u00a0 but sometimes we have to think whether to have a callback function or promises. Here we will be discussing about promises and call back function. So let have look on both callback function and promises and how it works. Call back function Callback functions are functions &hellip;\" \/>\n<meta property=\"og:url\" content=\"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/\" \/>\n<meta property=\"og:site_name\" content=\"shabeeb Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-08-21T05:27:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-30T06:45:00+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"shabeeb\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/shabeebk.com\/blog\/#website\",\"url\":\"http:\/\/shabeebk.com\/blog\/\",\"name\":\"shabeeb Blog\",\"description\":\"A developer blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/shabeebk.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/#webpage\",\"url\":\"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/\",\"name\":\"promises and call back - shabeeb Blog\",\"isPartOf\":{\"@id\":\"http:\/\/shabeebk.com\/blog\/#website\"},\"datePublished\":\"2015-08-21T05:27:01+00:00\",\"dateModified\":\"2022-03-30T06:45:00+00:00\",\"author\":{\"@id\":\"http:\/\/shabeebk.com\/blog\/#\/schema\/person\/71832abe654179971635c65c09bceb29\"},\"breadcrumb\":{\"@id\":\"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/shabeebk.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"promises and call back\"}]},{\"@type\":\"Person\",\"@id\":\"http:\/\/shabeebk.com\/blog\/#\/schema\/person\/71832abe654179971635c65c09bceb29\",\"name\":\"shabeeb\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/shabeebk.com\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/0.gravatar.com\/avatar\/9998389cc76a77663881c48f7d4cbba0?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/0.gravatar.com\/avatar\/9998389cc76a77663881c48f7d4cbba0?s=96&d=mm&r=g\",\"caption\":\"shabeeb\"},\"description\":\"Developer,Entrepreneur, software engineer more than that a human being\",\"sameAs\":[\"http:\/\/shabeebk.com\/blog\"],\"url\":\"http:\/\/shabeebk.com\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"promises and call back - shabeeb Blog","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":"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/","og_locale":"en_US","og_type":"article","og_title":"promises and call back - shabeeb Blog","og_description":"promises and call back are only doing similar job,\u00a0 but sometimes we have to think whether to have a callback function or promises. Here we will be discussing about promises and call back function. So let have look on both callback function and promises and how it works. Call back function Callback functions are functions &hellip;","og_url":"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/","og_site_name":"shabeeb Blog","article_published_time":"2015-08-21T05:27:01+00:00","article_modified_time":"2022-03-30T06:45:00+00:00","twitter_misc":{"Written by":"shabeeb","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/shabeebk.com\/blog\/#website","url":"http:\/\/shabeebk.com\/blog\/","name":"shabeeb Blog","description":"A developer blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/shabeebk.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/#webpage","url":"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/","name":"promises and call back - shabeeb Blog","isPartOf":{"@id":"http:\/\/shabeebk.com\/blog\/#website"},"datePublished":"2015-08-21T05:27:01+00:00","dateModified":"2022-03-30T06:45:00+00:00","author":{"@id":"http:\/\/shabeebk.com\/blog\/#\/schema\/person\/71832abe654179971635c65c09bceb29"},"breadcrumb":{"@id":"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/shabeebk.com\/blog\/promises-and-call-back\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/shabeebk.com\/blog\/promises-and-call-back\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/shabeebk.com\/blog\/"},{"@type":"ListItem","position":2,"name":"promises and call back"}]},{"@type":"Person","@id":"http:\/\/shabeebk.com\/blog\/#\/schema\/person\/71832abe654179971635c65c09bceb29","name":"shabeeb","image":{"@type":"ImageObject","@id":"http:\/\/shabeebk.com\/blog\/#personlogo","inLanguage":"en-US","url":"http:\/\/0.gravatar.com\/avatar\/9998389cc76a77663881c48f7d4cbba0?s=96&d=mm&r=g","contentUrl":"http:\/\/0.gravatar.com\/avatar\/9998389cc76a77663881c48f7d4cbba0?s=96&d=mm&r=g","caption":"shabeeb"},"description":"Developer,Entrepreneur, software engineer more than that a human being","sameAs":["http:\/\/shabeebk.com\/blog"],"url":"http:\/\/shabeebk.com\/blog\/author\/admin\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/shabeebk.com\/blog\/wp-json\/wp\/v2\/posts\/449"}],"collection":[{"href":"http:\/\/shabeebk.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shabeebk.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shabeebk.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shabeebk.com\/blog\/wp-json\/wp\/v2\/comments?post=449"}],"version-history":[{"count":15,"href":"http:\/\/shabeebk.com\/blog\/wp-json\/wp\/v2\/posts\/449\/revisions"}],"predecessor-version":[{"id":660,"href":"http:\/\/shabeebk.com\/blog\/wp-json\/wp\/v2\/posts\/449\/revisions\/660"}],"wp:attachment":[{"href":"http:\/\/shabeebk.com\/blog\/wp-json\/wp\/v2\/media?parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shabeebk.com\/blog\/wp-json\/wp\/v2\/categories?post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shabeebk.com\/blog\/wp-json\/wp\/v2\/tags?post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}