title: How do I wait for a promise to finish before returning the variable of a function?
body: <p>I'm still struggling with promises, but making some progress thanks to the community here.</p>
  
  <p>I have a simple JS function which queries a Parse database. It's supposed to return the array of results, but obviously due to the asynchronous nature of the query (hence the promises), the function returns before the results, leaving me with an undefined array.</p>
  
  <p>What do I need to do to make this function wait for the result of the promise?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>function resultsByName(name)
  {   
      var Card = Parse.Object.extend("Card");
      var query = new Parse.Query(Card);
      query.equalTo("name", name.toString());
  
      var resultsArray = [];
  
      var promise = query.find({
                 success: function(results) {
                 // results is an array of Parse.Object.
                               console.log(results);
                               //resultsArray = results;
                               return results;
                 },
  
                 error: function(error) {
                 // error is an instance of Parse.Error.
                               console.log("Error");
                 }
      });                           
  
  }
  </code></pre>