SailsJs, how can I make a model function a Promise so I can queue them in the controller?
<p>I may be a bit off base, but i need to make several api calls to gather the data needed to display on a view. </p>

<p>I have been reading about Q and Bluebird and their Promises but I cant seem to get it working, it continues on to render the view without getting the data from the model (async).</p>

<p>My question is(are):</p>

<ul>
<li>is this the best approach for getting multiple api calls? I would like to keep them in the model (MVC style)</li>
<li>should I be rendering each api call from the view so I can somehow have a loader image for the divs?</li>
<li>if I am on the right track, why is it not "waiting" on the data to be returned, or why is it not loading the data when it does become avaiable?</li>
</ul>

<p>ProvidersController.js</p>

<pre><code>var Promise = require("bluebird");

module.exports = {
  ...
  ...  
  findOne: function(req, res){
    var rest        = require('restler');
    var provider_id = req.params.id;

  if(!req.session.token){
    req.flash('error', 'No Token Present in Session');
    res.redirect("/user/login");
  }

  Promise.all([
    Providers.getData(1, req.session.token),
    //todo more model functions

  ]).spread(function(provider_promise, yetAnotherResult) {
    var data = {
     thing: "Lorem ipsum dolor sit.",
     provider: provider_promise,
     //otherthing: yetAnotherResult
  };
  console.log(data);
  return res.view({data: data});
});
</code></pre>

<p>Provider.js</p>

<pre><code>var Promise = require("bluebird");

module.exports = {

attributes: {
},

getData: function(provider_id, token){

  var rest        = require('restler');
    rest.get('HTTPS://THIS_URL'+provider_id, {
    headers: {'Authorization': 'Bearer '+ token},
  }).on('complete', function(data, response) {
    if (response.statusCode == 200) {
      console.log(JSON.parse(data).data);
      console.log("----Provider Model----");
      return JSON.parse(data).data;
    }
  });
}
};
</code></pre>

<p>when I hit the page, this is the output in the console</p>

<pre><code>{ thing: 'Lorem ipsum dolor sit.', provider: undefined }
{ type: 'provider',
 id: '1', attributes: { provider_id: 1, provider_name: 'Ron Somebody' }
}
----Provider Model----
</code></pre>

<p>here you can see that the console.log from the controller writes first, and the provider is undefined, but once the model function gets the data from the get request, it is wrote to the console correctly, along with the "---Provider Model---" flag </p>

<p>Thanks in advance for any guidance. </p>

<p>I bit closer, but the promises are still in the controller and id rather them be in the model..</p>

<pre><code>...
...
var request     = Promise.promisify(require('request'));

...
...
Promise.all([
  request({url: 'URL_1', headers: {'Authorization': 'Bearer '+ req.session.token } }),
  request({url: 'URL_2', headers: {'Authorization': 'Bearer '+ req.session.token } }),
  request({url: 'URL_3', headers: {'Authorization': 'Bearer '+ req.session.token } }),
]).spread(function(provider_obj, user_objs, classes_objs) {
  var data = {
    titleorpagedesc:  "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris gravida turpis et elit condimentum sit amet tempus tellus semper.",
    provider:         JSON.parse(provider_obj.body),
    users:            JSON.parse( user_objs.body ),
    classes:          JSON.parse( classes_objs.body )
  };
  // console.log(data);
  return res.view({data: data});
});
</code></pre>