title: Waiting for promise to resolve from parent function
body: <p>I have a primary thread in my node application such as this:</p>
  
  <pre><code>function main_thread() {
    console.log("Starting");
    values = get_values(1);
    console.log(values);
    console.log("I expect to be after the values");
  }
  </code></pre>
  
  <p>The <code>get_values</code> function calls the <code>hgetall</code> function using the <a href="https://github.com/NodeRedis/node_redis" rel="nofollow noreferrer">node_redis</a> package. This function provides a call back, but can be promisified:</p>
  
  <pre><code>function get_values(customer_id) {
    // Uses a callback for result
    new Promise(function(resolve, reject) {
      redis_client.hgetall(customer_id, function (err, result) {
      if (err) console.log(err)
        console.log("About to resolve");
        resolve(result);
      });
    })
    .then(({result}) =&gt; {
      console.log(result);
    });
  }
  </code></pre>
  
  <p>This works great for promise chaining within the function, however not so well in my main thread, as I can't wait and return the value.</p>
  
  <p>Here's how I'd do it in ruby, the main language I use:</p>
  
  <pre><code>def get_values(customer_id)
    return @redis_client.hgetall(customer_id)
  end
  </code></pre>
  
  <p>How can I create a promise within a reusable function and make the main thread wait until the function returns the response from the promise?</p>
  
  <p>EDIT:</p>
  
  <p>It's been suggested the promise can be returned with a <code>then</code> chained in the main thread. However this still means any code in the main thread after after the function call executes before the <code>then</code> block.</p>
  
  <p>EDIT 2:</p>
  
  <p>After lengthy discussion with some IRL JS developer friends, it looks like trying to create a synchronous script is against the ethos of modern JS. I'm going to go back to my application design and work on making it async.</p>