diff --git a/helpers/each.js b/helpers/each.js index b873952..502cf94 100644 --- a/helpers/each.js +++ b/helpers/each.js @@ -1,8 +1,7 @@ const { - appendContextPath, createFrame, blockParams, isPromise -} = require('../utils'), + appendContextPath, createFrame, blockParams, isPromise + } = require('../utils'), { Readable } = require('stream') -const { resolve } = require('eslint-plugin-promise/rules/lib/promise-statics') module.exports = (handlebars) => { handlebars.registerHelper('each', async function(context, options) { @@ -11,11 +10,11 @@ module.exports = (handlebars) => { } let { fn } = options, - { inverse } = options, - i = 0, - ret = [], - data, - contextPath + { inverse } = options, + i = 0, + ret = [], + data, + contextPath if (options.data && options.ids) { contextPath = `${appendContextPath(options.data.contextPath, options.ids[0])}.` @@ -41,7 +40,7 @@ module.exports = (handlebars) => { } } - ret.push(fn(context[field], { + ret.push(await fn(context[field], { data, blockParams: blockParams( [context[field], field], @@ -57,28 +56,28 @@ module.exports = (handlebars) => { if (Array.isArray(context)) { for (let j = context.length; i < j; i++) { if (i in context) { - execIteration(i, i, i === context.length - 1) + await execIteration(i, i, i === context.length - 1) } } } else if (global.Symbol && context[global.Symbol.iterator]) { const newContext = [], - iterator = context[global.Symbol.iterator]() + iterator = context[global.Symbol.iterator]() for (let it = iterator.next(); !it.done; it = iterator.next()) { newContext.push(it.value) } context = newContext for (let j = context.length; i < j; i++) { - execIteration(i, i, i === context.length - 1) + await execIteration(i, i, i === context.length - 1) } } else if (context instanceof Readable) { const newContext = [] await new Promise((resolve, reject) => { context.on('data', (item) => { newContext.push(item) - }).on('end', () => { + }).on('end', async() => { context = newContext for (let j = context.length; i < j; i++) { - execIteration(i, i, i === context.length - 1) + await execIteration(i, i, i === context.length - 1) } resolve() }).once('error', e => reject(e)) @@ -91,13 +90,13 @@ module.exports = (handlebars) => { // the last iteration without have to scan the object twice and create // an itermediate keys array. if (priorKey !== undefined) { - execIteration(priorKey, i - 1) + await execIteration(priorKey, i - 1) } priorKey = key i++ } if (priorKey !== undefined) { - execIteration(priorKey, i - 1, true) + await execIteration(priorKey, i - 1, true) } } } @@ -106,6 +105,6 @@ module.exports = (handlebars) => { ret = inverse(this) } - return (await Promise.all(ret)).join('') + return ret.join('') }) } diff --git a/package.json b/package.json index 1145a57..fc51e72 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "handlebars-async-helpers", - "version": "1.0.0", + "version": "1.0.1", "description": "Adding async function support to handlebars helpers", "main": "index.js", "scripts": { diff --git a/test/test.js b/test/test.js index e13610a..076d9c3 100644 --- a/test/test.js +++ b/test/test.js @@ -10,7 +10,7 @@ describe('Test async helpers', () => { const hbs = asyncHelpers(Handlebars) hbs.registerHelper('sleep', async () => new Promise((resolve) => { - setTimeout(() => resolve('Done!'), 1000) + setTimeout(() => resolve('Done!'), 50) })) const result = await hbs.compile('Mark when is completed: {{#sleep}}{{/sleep}}')() @@ -22,7 +22,7 @@ describe('Test async helpers', () => { const hbs = asyncHelpers(Handlebars) hbs.registerPartial('myPartial', 'Some text: {{#sleep}}{{/sleep}}') hbs.registerHelper('sleep', async () => new Promise((resolve) => { - setTimeout(() => resolve('Done!'), 1000) + setTimeout(() => resolve('Done!'), 50) })) const result = await hbs.compile('Mark when is completed: {{> myPartial}}')() @@ -33,7 +33,7 @@ describe('Test async helpers', () => { it('Test each helper with async helpers inside', async () => { const hbs = asyncHelpers(Handlebars) hbs.registerHelper('sleep', async () => new Promise((resolve) => { - setTimeout(() => resolve('Done!'), 1000) + setTimeout(() => resolve('Done!'), 50) })) const items = [ @@ -62,7 +62,7 @@ describe('Test async helpers', () => { 'Drew' ]) hbs.registerHelper('getArray', () => new Promise((resolve) => { - setTimeout(() => resolve(items), 1000) + setTimeout(() => resolve(items), 50) })) const result = await hbs.compile('Devs \n{{#each (getArray items)}}Dev: {{this}}\n{{/each}}')({items}) @@ -98,7 +98,7 @@ describe('Test async helpers', () => { ` hbs.registerHelper('ipInfo', async () => { await new Promise((resolve) => { - setTimeout(resolve, 500) + setTimeout(resolve, 50) }) return {country: 'Canada'} }) @@ -120,7 +120,7 @@ describe('Test async helpers', () => { expected = `
Show
` hbs.registerHelper('shouldShow', async () => { await new Promise((resolve) => { - setTimeout(resolve, 500) + setTimeout(resolve, 50) }) return true }) @@ -137,7 +137,7 @@ describe('Test async helpers', () => { expected = `Show
` hbs.registerHelper('shouldShow', async () => { await new Promise((resolve) => { - setTimeout(resolve, 500) + setTimeout(resolve, 50) }) return null }) @@ -151,7 +151,7 @@ describe('Test async helpers', () => { it('Test with custom helpers and complex replacements', async() => { const timeout = ms => new Promise(res => setTimeout(res, ms)), delay = async function delayFn() { - await timeout(100) + await timeout(50) return 1000 }, hbs = asyncHelpers(Handlebars) @@ -224,7 +224,7 @@ describe('Test async helpers', () => { hbs.registerHelper('delay', delay) hbs.registerHelper('cursor', async(options) => { - await timeout(1000) + await timeout(50) return [{ name: 'test' }, { name: 'test2' }] }) @@ -280,7 +280,7 @@ describe('Test async helpers', () => { const hbs = asyncHelpers(Handlebars), template = 'Grand Child: {{#delayed 500}}{{/delayed}}
', + grandChild = 'Grand Child: {{#delayed 50}}{{/delayed}}
', expected = 'Grand Child: Hello!