title: generators, promises and co or then-yield
body: <p>I am writing a nodejs program and need to get some config data during initialization time so i can instantiate a singleton. The config data comes back as a promise, which means I have to initialise things on the fly, rather than at require time so i can <code>module.exports = () =&gt; return new blah(config)</code>. I dont want a factory to return a thenable - I want it to return a new instance of a constructor.</p>
  
  <p>I have an internal library that gets configuration data and returns a promise. I'm trying to use <code>co</code> or <code>then-yield</code> or a similar library so that I can treat things as if they are synchronous (i <em>think</em> this is what these libs are for, but perhaps my understanding is flawed), mostly so I can make a factory that returns an instance, and doesn't return an promise that resolves with the instance</p>
  
  <p>simplified code:</p>
  
  <p>config reader:</p>
  
  <pre><code>var read = function () {
    let c = projectConfig.load(); //returns a promise that resolves with config data
    return c;
  });
  
  module.exports = {read};
  </code></pre>
  
  <p>factory:</p>
  
  <pre><code>let factory = ty.async(function * factory () {
    let cfg = yield configHelper.read();
    console.log('config', cfg)
    return new Constructor(cfg);
  })
  
  module.exports = () =&gt; factory()
  </code></pre>
  
  <p>index:</p>
  
  <pre><code>let factory = require('factory')()
  console.log(factory)
  </code></pre>
  
  <p>When I run the program I get the following output</p>
  
  <pre><code>Promise {
      _d: {
          p: [Circular],
          c: [],
          a: undefined,
          s: 0,
          d: false,
          v: { cdn: [Object] },
          h: false,
          n: false
      }
  } // this is from the main module
  
  config { /* correct config object data here */ } //this is from factory
  </code></pre>
  
  <p>looking at various blog posts talking about this, I would expect that by using then-yield or co, <code>factory</code> should return the Constructor with the correct config params. but it looks like it co or ty is doing nothing, and im getting the yield result.</p>
  
  <p>What have i misunderstood?</p>