Skip to content

Commit 00f19c6

Browse files
committed
thunkify accepts Promise arguments
1 parent 2098fd5 commit 00f19c6

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

_internal/funcApply.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
* Apply arguments to a function.
1414
*/
1515
const funcApply = (func, args) => func(...args)
16+
17+
module.exports = funcApply

test.js

+6
Original file line numberDiff line numberDiff line change
@@ -3837,6 +3837,12 @@ eq(
38373837
assert.strictEqual(thunkAdd212.length, 0)
38383838
assert.strictEqual(thunkAdd212(), 3)
38393839
})
3840+
3841+
const asyncThunkAdd212 = thunkify(add2, Promise.resolve(1), 2)
3842+
it('creates a thunk that resolves any promise arguments', async () => {
3843+
assert.strictEqual(asyncThunkAdd212.length, 0)
3844+
assert.strictEqual(await asyncThunkAdd212(), 3)
3845+
})
38403846
})
38413847

38423848
describe('always', () => {

thunkify.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
const areAnyValuesPromises = require('./_internal/areAnyValuesPromises')
2+
const promiseAll = require('./_internal/promiseAll')
3+
const curry2 = require('./_internal/curry2')
4+
const __ = require('./_internal/placeholder')
5+
const funcApply = require('./_internal/funcApply')
6+
17
/**
28
* @name thunkify
39
*
@@ -18,6 +24,9 @@
1824
* ```
1925
*/
2026
const thunkify = (func, ...args) => function thunk() {
27+
if (areAnyValuesPromises(args)) {
28+
return promiseAll(args).then(curry2(funcApply, func, __))
29+
}
2130
return func(...args)
2231
}
2332

0 commit comments

Comments
 (0)