Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A <strong>promise</strong> represents a future value, usually a future result of an asynchronous operation, and allows us to define what will happen once this value becomes available, or when an error occurs.</p> <p><strong>Reference</strong></p> <pre><code>var promise = asyncFunction(parameters); promise.then( function (result) { // Do Something with the result }, function (error) { // Handle error (exception, etc). }); </code></pre> <p>You can wrap any value with a promise, using the utility method <code>$q.when()</code>. </p> <ul> <li>$q.when(promise) → promise </li> <li>$q.when(nonPromise) → a new promise, that will </li> <li>asynchronously resolve to the given value nonPromise. </li> </ul> <p><strong>==============================</strong></p> <p>In your example:</p> <p><em>HTML</em></p> <pre><code>&lt;div ng-app ng-controller="testController"&gt; &lt;p&gt;Direct property: {{ property }}&lt;/p&gt; &lt;p&gt;Function returning property: {{ getProperty() }}&lt;/p&gt; &lt;p&gt;Direct promise: {{ promise }}&lt;/p&gt; &lt;p&gt;Function returning promise: {{ out }}&lt;/p&gt; &lt;/div&gt; </code></pre> <p><em>JS</em></p> <pre><code>function testController($scope, $q) { var deferred = $q.defer(); deferred.resolve('some lazy text'); $scope.promise = deferred.promise; $scope.out = $scope.promise.then(function (result) { console.log(result); return result; }); $scope.property = 'some text'; $scope.getProperty = function () { return $scope.property; }; } </code></pre> <p>Demo <a href="http://jsfiddle.net/KnTtv/2/" rel="nofollow">Fiddle</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload