mikeym
mikeym

Reputation: 6331

Promise is resolving but not triggering .then() function in Node

Having a problem with getting the then of a function to trigger once a promise is resolved. I can see the resulting html in the console log of the nunjucks callback in the code below so I know it's working fine to that point. However nothing is coming back in the then of the calling function. Any ideas what the problem is? Thanks in advance!

function generate(data, schema, partials) {
  var formTitle = schema.title;


  var defered = q.defer();

  nunjucks.render('test.html', { formTitle: formTitle },function(err, 
html) { 

    if (err) {
      console.log('nunjucks error ', err);
      return defered.reject();
    }
    console.log('nunjucks render ok..', html); // html logging fine here
   // This seems not to work
    q.resolve(html);
  });


  return defered.promise;

}

This is the function call.

formTemplater.generate(data, schema, {
    header: fs.readFileSync('./header.html', 'utf8'), 
    footer: fs.readFileSync('./footer.html', 'utf8')
}).then(function(html) {
    // nothing works here
    console.log('nunjucks back with html :: ', html);
    fs.writeFileSync('./results.html');
});

Upvotes: 1

Views: 412

Answers (1)

robertklep
robertklep

Reputation: 203419

This:

q.resolve(html)

Should be this:

defered.resolve(html)

Upvotes: 5

Related Questions