promised-handlebars icon indicating copy to clipboard operation
promised-handlebars copied to clipboard

Block-helper `options.fn()` can return promise in some cases.

Open nknapp opened this issue 10 years ago • 2 comments

In the following example, options.fn() returns a promise instead of a string.

var promisedHandlebars = require('../')
var Q = require('q')
var Handlebars = promisedHandlebars(require('handlebars'))

// Register a helper that returns a promise for a boolean
Handlebars.registerHelper('eventually-true', function () {
  return Q.delay(100).then(function () {
    return true
  })
})

// Trim whitespaces from block-content result.
Handlebars.registerHelper('trim', function(options) {
  return options.fn().trim();
});

var template = Handlebars.compile('{{#trim}}{{#if (eventually-true)}}   abc{{/if}}{{/trim}}')

// The whole compiled function returns a promise as well
template({}).done(console.log)

The if-helper returns a promise because it has a promise as argument. This promise has a toString()-method that creates a placeholder, but it does not have a .trim() method.

nknapp avatar Aug 16 '15 20:08 nknapp

The solution would be to check if the helper is used as a block helper and then convert the return value to a string in the helper wrapper. This implies the assumption that block-helpers can never be used in other helpers arguments (which is probably true).

However, simple helpers must be able to return promises, which can be seen in the same example.

nknapp avatar Aug 17 '15 04:08 nknapp

After thinking about it some more, this is probably unfixable. It will be kept open to be visible to users who have similar problems.

nknapp avatar Aug 26 '16 23:08 nknapp