Describe the use of the $q service in AngularJS.
Instruction: Explain how the $q service is used for promise-based asynchronous programming in AngularJS.
Context: This question assesses the candidate's knowledge of handling asynchronous operations in AngularJS applications using promises.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
The AngularJS $q service creates and composes promises whose callbacks integrate with AngularJS change detection. A promise can be pending, fulfilled or rejected; chaining returns a new promise that adopts the returned value or promise, or rejects when its handler throws.
Use $q.when to normalize a value or thenable, $q.reject to return a rejection, and $q.all to combine results. Avoid wrapping $http in a new deferred merely to forward the promise it already returns...