简体   繁体   中英

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');
});

This:

q.resolve(html)

Should be this:

defered.resolve(html)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM