title: Got promise not working
body: <p>I'm trying to use promise to get in promise2</p>
  
  <p>But if I have an object Widgets with several elements in it...</p>
  
  <p>Why can't I have been able to get my console.log's output </p>
  
  <pre><code>Parse.Cloud.define("extract", function(request, response) {
      var user = request.params.user;
      var promise = Parse.Promise.as();
      [...]
      }).then(function() {
  
          return query.find().then(function(results) {  
              _.each(results, function(result) {
                  [...]
  
  
                  Widget.objectId = result.id;
                  Widgets[timestamp] = Widget;
              });
              return promise;
          }).then(function(results) {    
  
              for (var key in Widgets) {
                  var Widget = Widgets[key];
                  var widget_data =  Widgets[key].widget_data;
                  var promise2 = Parse.Promise.as();    
                  promise2 = promise2.then(function() {    
                   return Parse.Cloud.run('extractWidgetData', {
                          'widget_data': widget_data,
                      }).then(function(newresult) {
                          Widgets[key].data = newresult.data;
  
                          console.log('--------WHY NOT HERE ALL TIME ?--------');                              
                      });
                  });
  
                  return promise2;
              }
          }).then(function() {                
              response.success(Widgets);
              },
              function(error) {
                  response.error("Error: " + error.code + " " + error.message);
              });
      });
  });
  </code></pre>
  
  <p>I'm becoming crazy to run this damn Code</p>
  
  <p><strong>EDIT</strong> : I finally followed Roamer's advices to implement something but I'm not sure if it's the good way to work with Promise in series...</p>
  
  <pre><code>Parse.Cloud.define("extract", function(request, response) {
      var user = request.params.user;
      var Widgets = {};
      ...
      ... .then(function() {
          return query.find().then(function(results) {
              return Parse.Promise.when(results.map(function(result) {
                  var Widget  = ...;//some transform of `result`
                  Widget.id = ...;//some transform of `result`
                  var timestamp = createdAtDate.getTime();
                  ...
                  return Parse.Cloud.run('extractData', {
                      'widget_data': Widget.widget_data,
                  }).then(function(newresult) {
                      Widget.stat = newresult.stats;
                      return Widget;//&lt;&lt;&lt;&lt;&lt;&lt;&lt; important! This ensures that results.map() returns an array of promises, each of which delivers a Widget objects.
                  });                                
              }));
      }).then(function() {              
          var promisedWidget = Array.prototype.slice.apply(arguments);       
          return Parse.Promise.when(promisedWidget.map(function(Widget) {
              return Parse.Cloud.run('getWineStats', {
                  'id': Widget.data.id
              }).then(function(stat) {
                  Widget.stat = stat;
                  return Widget;
              });                
          }));
      }).then(function() {               
          var promisedWidget = Array.prototype.slice.apply(arguments); 
  
          _.each(promisedWidget, function(Widget) {            
              var createdAtObject = Widget.createdAt;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
              var strDate = createdAtObject.toString();
              var createdAtDate = new Date(strDate);
              timestamp = createdAtDate.getTime();
              Widgets[timestamp] = Widget;
          });   
  
          return Widgets;            
      }).then(function(Widgets) {               
                  response.success(Widgets);
              },
              function(error) {
                  response.error("Error: " + error.code + " " + error.message);
              });
      });            
  
  });
  </code></pre>