{"id":11701,"date":"2016-04-04T12:11:10","date_gmt":"2016-04-04T09:11:10","guid":{"rendered":"https:\/\/www.webcodegeeks.com\/?p=11701"},"modified":"2016-03-24T10:52:31","modified_gmt":"2016-03-24T08:52:31","slug":"promises-angularjs-part-ii-q-service","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/","title":{"rendered":"Promises in AngularJS. Part II. $q service."},"content":{"rendered":"<p>I have already <a href=\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-basics\/\">blogged about Promises in AngularJS 1.x<\/a>. This is the second part which describes the Angular&#8217;s <code>$q<\/code> service.<\/p>\n<p>The <code>$q<\/code> service can be used in two different ways. The first way mimics the <a href=\"https:\/\/github.com\/kriskowal\/q\">Q library<\/a> for creating and composing asynchronous promises in JavaScript.<\/p>\n<p>The second way mimics the ECMAScript 2015 (ES6) style. Let&#8217;s begin with the first way.<\/p>\n<p>First of all, you have to create a <code>deferred<\/code> object by <code>$q.defer()<\/code>.<\/p>\n<pre class=\"brush:js\"> \r\nvar deferred = $q.defer();\r\n<\/pre>\n<p>A <code>deferred<\/code> object can be created within an asynchronous function. The function should return a promise object created from the <code>deferred<\/code> object as follows:<\/p>\n<pre class=\"brush:js\"> \r\nreturn deferred.promise;\r\n<\/pre>\n<p>A promise is always in either one of three states:<\/p>\n<ol>\n<li><code>Pending<\/code>: the result hasn&#8217;t been computed yet<\/li>\n<li><code>Fulfilled<\/code>: the result was computed successfully<\/li>\n<li><code>Rejected<\/code>: a failure occurred during computation<\/li>\n<\/ol>\n<p>When the asynchronous function finished the execution, it can invoke one of the two methods:<\/p>\n<pre class=\"brush:js\">deferred.resolve(...)\r\ndeferred.reject(...)<\/pre>\n<p>The first call <code>deferred.resolve(...<\/code>) puts the promise into the fulfilled state. As result a <code>success callback<\/code> will be invoked. The second call <code>deferred.reject(...)<\/code> puts the promise into the rejected state. As result an <code>error callback<\/code> will be invoked. It is also possible to invoke<\/p>\n<pre class=\"brush:js\"> \r\ndeferred.notify(...)\r\n<\/pre>\n<p>during the function&#8217;s execution to propogate some progress from the asynchronous function to an <code>update callback<\/code>. All three callbacks can be registered on the promise as parameters of the function <code>then<\/code>:<\/p>\n<pre class=\"brush:js\">var promise = someAsynchronousFunction();\r\npromise.then(function(value) {\r\n    \/\/ success\r\n    ...\r\n}, function(reason) {\r\n    \/\/ failure\r\n    ...\r\n}, function(update) {\r\n    \/\/ update\r\n    ...\r\n});<\/pre>\n<p>Let&#8217;s implement an example. We will take <code>setTimeout()<\/code> as an asynchronous function. In the real application, you will probably use some other asynchronous services. In the <code>setTimeout()<\/code>, we will generate a random number after 1 sek. If the number is less than 0.5, we will invoke <code>deferred.resolve(random)<\/code>, otherwise <code>deferred.reject(random)<\/code>. The entire logic is implemented in the controller <code>PromiseController<\/code>.<\/p>\n<pre class=\"brush:js\">var app = angular.module('app', []);\r\napp.controller('PromiseController', PromiseController);\r\n\r\nfunction PromiseController($q) {\r\n    var _self = this;\r\n    this.message = null;\r\n\r\n    var asyncFunction = function() {\r\n        var deferred = $q.defer();\r\n\r\n        setTimeout(function() {\r\n            var random = Math.random().toFixed(2);\r\n\r\n            if (random &lt; 0.5) {\r\n                deferred.resolve(random);\r\n            } else {\r\n                deferred.reject(random);\r\n            }\r\n        }, 1000);\r\n\r\n        return deferred.promise;\r\n    }\r\n\r\n    this.invokeAsyncFunction = function() {\r\n        var promise = asyncFunction();\r\n\r\n        promise.then(function(message) {\r\n            _self.message = \"Success: \" + message;\r\n        }, function(message) {\r\n            _self.message = \"Error: \" + message;\r\n        });\r\n    }\r\n}<\/pre>\n<p>As you can see, the asynchronous function <code>asyncFunction<\/code> is invoked in the controller&#8217;s method <code>invokeAsyncFunction<\/code>. The function <code>asyncFunction<\/code> returns a promise. In the success case, the promise gets fulfilled and the first registered <code>success callback<\/code> gets executed. In the error case, the promise gets rejected and the second registered <code>error callback<\/code> gets executed. The <code>invokeAsyncFunction<\/code> is bound to the onclick event on a button.<\/p>\n<pre class=\"brush:xml\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=\"utf-8\" \/&gt;\r\n    &lt;meta content=\"IE=edge\" http-equiv=\"X-UA-Compatible\" \/&gt;\r\n    &lt;script src=\"https:\/\/code.angularjs.org\/1.4.8\/angular.js\"&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n\r\n&lt;body ng-app=\"app\" ng-controller=\"PromiseController as ctrlPromise\"&gt;\r\n    &lt;div ng-bind=\"ctrlPromise.message\"&gt;&lt;\/div&gt;\r\n    &lt;p&gt;&lt;\/p&gt;\r\n    &lt;button ng-click=\"ctrlPromise.invokeAsyncFunction()\"&gt;\r\n        Invoke asynchronous function\r\n    &lt;\/button&gt;\r\n\r\n    &lt;script src=\"controller.js\"&gt;&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>The message in GUI looks like as follows (success case as example):<\/p>\n<p><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/asyncFunction.png\" rel=\"attachment wp-att-11710\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-11710\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/asyncFunction.png\" alt=\"asyncFunction\" width=\"306\" height=\"109\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/asyncFunction.png 306w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/asyncFunction-300x107.png 300w\" sizes=\"(max-width: 306px) 100vw, 306px\" \/><\/a><\/p>\n<p>The Plunker of this example is <a href=\"http:\/\/plnkr.co\/edit\/HClkdYX3RxvjUwsu4aCn?p=preview\">available here<\/a>. Here is another picture that visualize the relation between methods of the deferred object:<\/p>\n<p><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/promises.png\" rel=\"attachment wp-att-11711\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-11711\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/promises.png\" alt=\"promises\" width=\"500\" height=\"170\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/promises.png 500w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/promises-300x102.png 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>As you can see, the asynchronous function doesn&#8217;t return the <code>deferred<\/code> object directly. The reason for that is obvious. If the <code>deferred<\/code> object would be returned instead of <code>deferred.promise<\/code>, the caller of the asynchronous function could be able to trigger callbacks by invoking <code>deferred.resolve(...)<\/code>, <code>deferred.reject(...)<\/code> or d<code>eferred.notify(...)<\/code>. For this reason these methods are protected from being invoking from outside by the caller.<\/p>\n<p>The example above can be rewritten in the ECMAScript 2015 (ES6) style (I mentioned this way at the beginning). A promise in ECMAScript 2015 can be created as an instance of <code>Promise<\/code> object.<\/p>\n<pre class=\"brush:js\">var promise = new Promise(function(resolve, reject) {\r\n    ...\r\n    if(...) {\r\n        resolve(value); \/\/ success\r\n    } else {\r\n        reject(reason); \/\/ failure\r\n    }\r\n});<\/pre>\n<p>Our <code>asyncFunction<\/code> function looks in this case as follows:<\/p>\n<pre class=\"brush:js\">var asyncFunction = function() {\r\n    return $q(function(resolve, reject) {\r\n\r\n        setTimeout(function() {\r\n            var random = Math.random().toFixed(2);\r\n\r\n            if (random &lt; 0.5) {\r\n                resolve(random);\r\n            } else {\r\n                reject(random);\r\n            }\r\n        }, 1000);\r\n    });\r\n}<\/pre>\n<p>The remaining code stays unchanged. Let&#8217;s go on. The next question is, how can we produce a rejection in success or error callbacks? For instance, you check some condition in a callback and want to produce an error if the condition is not fulfilled. Sometimes, we also want to forward rejection in a chain of promises. That means, you catch an error via an error callback and you want to forward the error to the promise derived from the current promise. There are two ways to achieve this qoal. The first one consists in using the <code>$q.reject(...)<\/code> like shown below.<\/p>\n<pre class=\"brush:js\">var promise = someAsynchronousFunction();\r\npromise.then(function(value) {\r\n    \/\/ success\r\n    ...\r\n    if(someCondition) {\r\n        $q.reject(\"An error occurred!\");\r\n    }\r\n}, function(reason) {\r\n    \/\/ failure\r\n    ...\r\n}).catch(function(error) {\r\n    \/\/ do something in error case\r\n    ...\r\n});<\/pre>\n<p>In our example, we will adjust the function <code>invokeAsyncFunction<\/code> in order to check very small values (smaller than 0.1).<\/p>\n<pre class=\"brush:js\">this.invokeAsyncFunction = function() {\r\n    var promise = asyncFunction();\r\n\r\n    promise.then(function(random) {\r\n        if (random &lt; 0.1) {\r\n            return $q.reject(\"Very small random value!\");\r\n        }\r\n\r\n        _self.message = \"Success: \" + random;\r\n    }, function(random) {\r\n        _self.message = \"Error: \" + random;\r\n    }).catch(function(error) {\r\n        _self.message = \"Special error: \" + error;\r\n    });\r\n}<\/pre>\n<p><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/asyncFunction2.png\" rel=\"attachment wp-att-11712\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-11712\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/asyncFunction2.png\" alt=\"asyncFunction2\" width=\"455\" height=\"119\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/asyncFunction2.png 455w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/asyncFunction2-300x78.png 300w\" sizes=\"(max-width: 455px) 100vw, 455px\" \/><\/a><\/p>\n<p>A Plunker example is <a href=\"http:\/\/plnkr.co\/edit\/clcvSYttO3lU69K5V5tc?p=preview\">available here<\/a>. Keep in mind the difference between <code>deferred.reject(...)<\/code> and <code>$q.reject(...)<\/code>. The call <code>deferred.reject(...)<\/code> puts the corresponding promise into the rejected state. The call <code>$q.reject(...)<\/code> creates a new promise which is already in the rejected state.<\/p>\n<p>The second way to produce a rejection in success or error callbacks consists in throwing an exception with <code>throw new Error(...)<\/code>.<\/p>\n<pre class=\"brush:js\">this.invokeAsyncFunction = function() {\r\n    var promise = asyncFunction();\r\n\r\n    promise.then(function(random) {\r\n        if (random &lt; 0.1) {\r\n            throw new Error(\"Very small random value!\");\r\n        }\r\n\r\n        _self.message = \"Success: \" + random;\r\n    }, function(random) {\r\n        _self.message = \"Error: \" + random;\r\n    }).catch(function(error) {\r\n        _self.message = \"Special error: \" + error;\r\n    });\r\n}<\/pre>\n<p>AngularJS will catch the exception, create a promise in the rejected state and forward it to the next block in the chain of promises. In the example, the error will be forwarded to the catch block. One downside of this approach with <code>throw new Error(...)<\/code> is that the error will be logged in the console. Picture from the Chrome Dev Tools:<\/p>\n<p><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/stackTrace.png\" rel=\"attachment wp-att-11713\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-11713\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/stackTrace.png\" alt=\"stackTrace\" width=\"500\" height=\"136\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/stackTrace.png 500w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/03\/stackTrace-300x82.png 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>But well, it works as designed and probably it is even an advantage to see thrown errors in the console.<\/p>\n<p>There is also an opposite method <code>$q.when(...)<\/code> which returns an immediately resolved promise. The documentation says: <code>$q.when(...)<\/code> &#8220;wraps an object that might be a value or a (3rd party) then-able promise into a <code>$q<\/code> promise. This is useful when you are dealing with an object that might or might not be a promise, or if the promise comes from a source that can&#8217;t be trusted.&#8221; You can e.g. wrap an <a href=\"https:\/\/api.jquery.com\/category\/deferred-object\/\">jQuery Deferred Object<\/a> with <code>$q.when(...)<\/code> or simple write<\/p>\n<pre class=\"brush:js\">$q.when(\"Finished!\").then(\r\n    function handleResolve(value) {\r\n        console.log(\"Resolved with value: \", value);\r\n    }\r\n);<\/pre>\n<p>and see <code>Resolved with value: Finished<\/code>! in the console. The alias of <code>$q.when(value)<\/code> is <code>$q.resolve(value)<\/code>. This was introduced later in order to maintain naming consistency with ECMAScript 2015. Last but not least is the method <code>$q.all(promises)<\/code> where <code>promises<\/code> is an array of multiple promises. This call returns a single promise that is resolved when all promises in the given array gets resolved.<\/p>\n<pre class=\"brush:js\">var promise1 = someAsynchronousFunction1();\r\nvar promise2 = someAsynchronousFunction2();\r\n\r\n$q.all([promise1, promise2]).then(function(result) {\r\n    console.log(\"Promises \" + result[0] + \" and \" + result[1] + \" finished their work successfully\");\r\n});<\/pre>\n<p>As you can see, the <code>result<\/code> passed into the callback function is an array of two outcomes &#8211; the outcome of the first and the outcome of the second callback.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/ovaraksin.blogspot.com\/2016\/03\/promises-in-angularjs-part-ii-q-service.html\">Promises in AngularJS. Part II. q service.<\/a> from our <a href=\"http:\/\/www.webcodegeeks.com\/join-us\/wcg\/\">WCG partner<\/a> Oleg Varaksin at the <a href=\"http:\/\/ovaraksin.blogspot.com\/\">Thoughts on software development<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I have already blogged about Promises in AngularJS 1.x. This is the second part which describes the Angular&#8217;s $q service. The $q service can be used in two different ways. The first way mimics the Q library for creating and composing asynchronous promises in JavaScript. The second way mimics the ECMAScript 2015 (ES6) style. Let&#8217;s &hellip;<\/p>\n","protected":false},"author":15,"featured_media":909,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-11701","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular-js"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Promises in AngularJS. Part II. $q service. - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"I have already blogged about Promises in AngularJS 1.x. This is the second part which describes the Angular&#039;s $q service. The $q service can be used in\" \/>\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\/angular-js\/promises-angularjs-part-ii-q-service\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Promises in AngularJS. Part II. $q service. - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"I have already blogged about Promises in AngularJS 1.x. This is the second part which describes the Angular&#039;s $q service. The $q service can be used in\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/\" \/>\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=\"2016-04-04T09:11:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-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=\"Oleg Varaksin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Oleg Varaksin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/\"},\"author\":{\"name\":\"Oleg Varaksin\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/907f82ccdf11dbd8f7b6af2c1024d712\"},\"headline\":\"Promises in AngularJS. Part II. $q service.\",\"datePublished\":\"2016-04-04T09:11:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/\"},\"wordCount\":874,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"articleSection\":[\"Angular.js\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/\",\"name\":\"Promises in AngularJS. Part II. $q service. - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"datePublished\":\"2016-04-04T09:11:10+00:00\",\"description\":\"I have already blogged about Promises in AngularJS 1.x. This is the second part which describes the Angular's $q service. The $q service can be used in\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#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\":\"Angular.js\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/angular-js\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Promises in AngularJS. Part II. $q service.\"}]},{\"@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\/907f82ccdf11dbd8f7b6af2c1024d712\",\"name\":\"Oleg Varaksin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fda1ce47105421f7a352a13dbefec14ab59d59ffae99c6c3002e5841578979d3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fda1ce47105421f7a352a13dbefec14ab59d59ffae99c6c3002e5841578979d3?s=96&d=mm&r=g\",\"caption\":\"Oleg Varaksin\"},\"sameAs\":[\"http:\/\/ovaraksin.blogspot.com\/\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/oleg-varaksin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Promises in AngularJS. Part II. $q service. - Web Code Geeks - 2026","description":"I have already blogged about Promises in AngularJS 1.x. This is the second part which describes the Angular's $q service. The $q service can be used in","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\/angular-js\/promises-angularjs-part-ii-q-service\/","og_locale":"en_US","og_type":"article","og_title":"Promises in AngularJS. Part II. $q service. - Web Code Geeks - 2026","og_description":"I have already blogged about Promises in AngularJS 1.x. This is the second part which describes the Angular's $q service. The $q service can be used in","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2016-04-04T09:11:10+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","type":"image\/jpeg"}],"author":"Oleg Varaksin","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Oleg Varaksin","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/"},"author":{"name":"Oleg Varaksin","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/907f82ccdf11dbd8f7b6af2c1024d712"},"headline":"Promises in AngularJS. Part II. $q service.","datePublished":"2016-04-04T09:11:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/"},"wordCount":874,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","articleSection":["Angular.js"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/","name":"Promises in AngularJS. Part II. $q service. - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","datePublished":"2016-04-04T09:11:10+00:00","description":"I have already blogged about Promises in AngularJS 1.x. This is the second part which describes the Angular's $q service. The $q service can be used in","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/promises-angularjs-part-ii-q-service\/#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":"Angular.js","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/angular-js\/"},{"@type":"ListItem","position":4,"name":"Promises in AngularJS. Part II. $q service."}]},{"@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\/907f82ccdf11dbd8f7b6af2c1024d712","name":"Oleg Varaksin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/fda1ce47105421f7a352a13dbefec14ab59d59ffae99c6c3002e5841578979d3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fda1ce47105421f7a352a13dbefec14ab59d59ffae99c6c3002e5841578979d3?s=96&d=mm&r=g","caption":"Oleg Varaksin"},"sameAs":["http:\/\/ovaraksin.blogspot.com\/"],"url":"https:\/\/www.webcodegeeks.com\/author\/oleg-varaksin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/11701","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\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=11701"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/11701\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/909"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=11701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=11701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=11701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}