mde / ejs

Embedded JavaScript templates -- http://ejs.co
Apache License 2.0
7.7k stars 841 forks source link

Added async handling of the `includer` #709

Open N247S opened 1 year ago

N247S commented 1 year ago

This is a possible easy fix for the issue I reported yesterday.

I am not sure if the typescript declarations gets updated automatically, or if that takes additional actions, If more things are required to push this, feel free to ask.

This change allows for the ClientFunction and AsyncClientFunction's third argument (includer) to be both a normal and an async function (or a normal function returning a Promise instance). In fact everything inside a raw-output and escaped-output tag can be. Example:

function customIncluder(options, path, data)
{
  return fetch(path)
    .then((r) => r.text())
    .then((t) => {
      ejs.compile(t, options)(data, null, (ipath, idata) => customIncluder(options, ipath, idata));
    })
}

let data;
let options = {client: true, async: true};
ejs.compile("<%- include("test.ejs") _%>", options)(data, null, (ipath, idata) => customIncluder(options, ipath, idata));