Skip to content

Commit

Permalink
reduce a closure
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Sep 23, 2023
1 parent 808c313 commit a1de9da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion __tests__/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ test('a null value', async () => {
const result = await streamToString(render`__1__${promise1}__2__${promise2}__3__`)
assert.strictEqual(result, '__1____2____3__')
expect(result).toMatchSnapshot()
})
})
19 changes: 12 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ function render (template, ...values) {
}
}

Promise.resolve(value).then((result) => {
if (!result) return
if (typeof result === 'string') return stream.write(result)
if (result.readableObjectMode) throw new Error('Readable object streams are not supported.')
if (result.readable) return pipe(result)
throw new Error(`Value passed did not return a string or a stream: <${typeof result}>${JSON.stringify(result)}`)
}).catch(onError).finally(next)
Promise.resolve(value)
.then(onPromiseResolve)
.catch(onError)
.finally(next)
}

function onPromiseResolve (result) {
if (!result) return
if (typeof result === 'string') return stream.write(result)
if (result.readableObjectMode) throw new Error('Readable object streams are not supported.')
if (result.readable) return pipe(result)
throw new Error(`Value passed did not return a string or a stream: <${typeof result}>${JSON.stringify(result)}`)
}

function pipe (readable) {
Expand Down

0 comments on commit a1de9da

Please sign in to comment.