Skip to content

Commit

Permalink
feat: render async function
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowtime2000 committed Nov 28, 2020
1 parent e52d7e2 commit 487bf6c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> | void {
return render(template, data, { ...config, async: true }, cb)
}
13 changes: 12 additions & 1 deletion test/render.spec.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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 () => {
let template = 'Hello <%= await it.getName() %>!'
let getName = () => {
return new Promise((res) => {
setTimeout(() => {
res('Ada')
}, 20)
})
}
expect(await renderAsync(template, { getName })).toEqual('Hello Ada!')
})
})
})

Expand Down

0 comments on commit 487bf6c

Please sign in to comment.