diff --git a/src/file-handlers.ts b/src/file-handlers.ts index fd80bee..3fa4b47 100644 --- a/src/file-handlers.ts +++ b/src/file-handlers.ts @@ -241,4 +241,22 @@ function renderFile( return tryHandleCache(data, renderConfig, callback) } -export { includeFile, renderFile } +function renderFileAsync( + filename: string, + data: DataObj, + config?: PartialConfig, + cb?: CallbackFn +): Promise | void + +function renderFileAsync(filename: string, data: DataObj, cb?: CallbackFn): Promise | void + +function renderFileAsync( + filename: string, + data: DataObj, + config?: PartialConfig, + cb?: CallbackFn +): Promise | void { + return renderFile(filename, data, { ...config, async: true }, cb) +} + +export { includeFile, renderFile, renderFileAsync } diff --git a/src/index.ts b/src/index.ts index 055cceb..a175819 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import { config } from './config' config.includeFile = includeFileHelper config.filepathCache = {} -export { loadFile, renderFile, renderFile as __express } from './file-handlers' +export { loadFile, renderFile, renderFileAsync, renderFile as __express } from './file-handlers' /* End file stuff */ diff --git a/src/render.ts b/src/render.ts index df26126..30ab4c0 100644 --- a/src/render.ts +++ b/src/render.ts @@ -91,3 +91,7 @@ export default function render( return handleCache(template, options)(data, options) } } + +export function renderAsync(template: string | TemplateFunction, data: object, config?: PartialConfig, cb?: CallbackFn): string | Promise | void { + return render(template, data, { ...config, async: true }, cb) +} diff --git a/test/file-handlers.spec.ts b/test/file-handlers.spec.ts index 39354b1..fd93bba 100644 --- a/test/file-handlers.spec.ts +++ b/test/file-handlers.spec.ts @@ -1,6 +1,6 @@ /* global it, expect, describe */ -import { renderFile, __express, templates, compile } from '../src/index' +import { renderFile, renderFileAsync, __express, templates, compile } from '../src/index' import { buildRegEx } from './err.spec' @@ -137,6 +137,12 @@ if(cb){cb(null,tR)} return tR }) }) +describe('renderFileAsync function', () => { + it('should render properly', async () => { + expect(await renderFileAsync(filePath, { name: 'foo' })).toBe('Hi foo') + }) +}) + // NOTE: the errors will really look like this: /* diff --git a/test/render.spec.ts b/test/render.spec.ts index 048ee8f..0cac576 100644 --- a/test/render.spec.ts +++ b/test/render.spec.ts @@ -1,6 +1,6 @@ /* global it, expect, describe */ -import render from '../src/render' +import render, { renderAsync } from '../src/render' import compile from '../src/compile' import { templates } from '../src/containers' @@ -20,7 +20,7 @@ describe('Simple Render checks', () => { ) }) it('Rendering function works', async () => { - let template = 'Hello <%= await it.getName() %>!' + const template = 'Hello <%= await it.getName() %>!' let getName = () => { return new Promise((res) => { setTimeout(() => { @@ -30,6 +30,17 @@ describe('Simple Render checks', () => { } expect(await render(template, { getName: getName }, { async: true })).toEqual('Hello Ada!') }) + it('Rendering async function works', async () => { + const template = 'Hello <%= await it.getName() %>!' + let getName = () => { + return new Promise((res) => { + setTimeout(() => { + res('Ada') + }, 20) + }) + } + expect(await renderAsync(template, { getName })).toEqual('Hello Ada!') + }) }) })