Skip to content

Commit c54663f

Browse files
committed
feat(applyasync): add applyAsync function
1 parent 3203ecb commit c54663f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

index.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from "ava";
2-
import {allAsync, sequenceAsync} from "./index";
2+
import {allAsync, applyAsync, sequenceAsync} from "./index";
33

44
test("allAsync", async t => {
55
const a = Promise.resolve(1 as const);
@@ -52,3 +52,8 @@ test("sequenceAsync", async t => {
5252

5353
t.deepEqual(a, [1, 2]);
5454
});
55+
56+
test("applyAsync", async t => {
57+
const f = Promise.resolve((a: number) => a + 1);
58+
t.is(await applyAsync(f)(2), 3);
59+
});

index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ export async function sequenceAsync<T extends Iterable<() => unknown>>(
2929
}
3030
return result as SequenceAsync<T>;
3131
}
32+
33+
export function applyAsync<TA, TResult>(
34+
promise: Promise<(a: TA) => TResult>
35+
): (a: TA) => Promise<TResult> {
36+
return async a => promise.then(f => f(a));
37+
}

0 commit comments

Comments
 (0)