Answers.Body: <p>I finally remembered that promises return callbacks. So, instead of</p>
  <pre><code>return result
  </code></pre>
  <p>in the then of the crmApi promise, using</p>
  <pre><code>self.user result
  </code></pre>
  <p>does exactly what I need.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-27T20:56:46.447Z
Answers.LastActivityDate: 2020-06-27T20:56:46.447Z
Answers.UserDisplayName: Mike Steigerwald
Body: <p>Referring to the snippet below, how do I get the controller function (DashboardController) to 'wait' for the return from crmApi? I've debugged enough to know crmApi returns the results I like, but DashboardController exits before I set self.user.</p>
  <p>I'm weak on promises, but perhaps I also need to find a better place to call crmApi? Is it a better practice to call crmApi within, e.g., angular.module('tournament').config(['$routeProvider' ...</p>
  <p>angular.module('tournament').component('dashboard', {
  templateUrl: '~/tournament/dashboard/dashboard.template.html',
  controller: ['$routeParams', 'crmApi',
  function DashboardController($routeParams, crmApi) {
  var self = this;</p>
  <pre><code>  // e.g., dashboard/43
    if ($routeParams.userId !== undefined) self.userId = $routeParams.userId;
  
    self.user = crmApi('Contact', 'getsingle', {
      &quot;sequential&quot;: 1,
      &quot;return&quot;: [&quot;display_name&quot;,&quot;modified_date&quot;],
      &quot;id&quot;: self.userId
      }).then(
          // Success
          function(result) { 
              return result; // How can I get self.user to 'wait' for this return?
          },
          // Failure
          function (error) { 
              CRM.alert(ts('Could not get user record ID of %1, error = %2', {1: userId, 2: error}),ts('Not Found'),'error'); 
          }
      );
  }
  </code></pre>
  <p>]
  });</p>
  
Tags: <javascript><angular>
Title: How best to use crmApi result within angularJs controller?
ThreadText: # How best to use crmApi result within angularJs controller?
  
  (Asked by: username_0 on 2020-06-27)
  
  Referring to the snippet below, how do I get the controller function (DashboardController) to 'wait' for the return from crmApi? I've debugged enough to know crmApi returns the results I like, but DashboardController exits before I set self.user.
  
  I'm weak on promises, but perhaps I also need to find a better place to call crmApi? Is it a better practice to call crmApi within, e.g., angular.module('tournament').config(['$routeProvider' ...
  
  angular.module('tournament').component('dashboard', {
  templateUrl: '~/tournament/dashboard/dashboard.template.html',
  controller: ['$routeParams', 'crmApi',
  function DashboardController($routeParams, crmApi) {
  var self = this;
  
  ```
    // e.g., dashboard/43
    if ($routeParams.userId !== undefined) self.userId = $routeParams.userId;
  
    self.user = crmApi('Contact', 'getsingle', {
      "sequential": 1,
      "return": ["display_name","modified_date"],
      "id": self.userId
      }).then(
          // Success
          function(result) { 
              return result; // How can I get self.user to 'wait' for this return?
          },
          // Failure
          function (error) { 
              CRM.alert(ts('Could not get user record ID of %1, error = %2', {1: userId, 2: error}),ts('Not Found'),'error'); 
          }
      );
  }
  
  ```
  
  ]
  });
  
  **Tags:** javascript, angular
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-06-27. Score: 1
  
  I finally remembered that promises return callbacks. So, instead of
  
  ```
  return result
  
  ```
  
  in the then of the crmApi promise, using
  
  ```
  self.user result
  
  ```
  
  does exactly what I need.
  
  
  ---
  
  