简体   繁体   English

Promise正在解决但未触发Node中的.then()函数

[英]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. 一旦解决了诺言,就无法触发函数的then。 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. 我可以在下面的代码中的nunjucks回调的console log中看到生成的html,因此我知道目前为止工作正常。 However nothing is coming back in the then of the calling function. 但是什么是回来的then调用函数。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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