-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
30 lines (29 loc) · 1.03 KB
/
index.ts
File metadata and controls
30 lines (29 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Utility function to create a non-blocking server action that can be invoked with {@link runParallelAction}.
* Learn more at https://github.com/icflorescu/next-server-actions-parallel.
*
* @example
* const listUsers = createParallelAction(async () => { // 👈 don't forget the `async` keyword
* return await prisma.user.findMany();
* });
*
* const listProducts = createParallelAction(async () => {
* return await prisma.product.findMany();
})
*/
export function createParallelAction<T, U extends unknown[]>(action: (...args: U) => Promise<T>) {
return async (...args: U) => [action(...args)] as const;
}
/**
* Utility function to invoke a non-blocking server action created with {@link createParallelAction}.
* Learn more at https://github.com/icflorescu/next-server-actions-parallel.
*
* @example
* await Promise.all([
* runParallelAction(listUsers()),
* runParallelAction(listProducts())
* ]);
*/
export async function runParallelAction<T>(result: Promise<readonly [Promise<T>]>) {
return (await result)[0];
}