Skip to content

Commit

Permalink
fix ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Jul 19, 2024
1 parent b824d39 commit c9134ae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/helpers/seriesAll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describe("seriesAll", () => {
Promise.resolve(1),
sleep(1).then(() => 2),
() => Promise.resolve(3),
() => 4,
async () => 5,
async () => {
await sleep(1);
Expand All @@ -19,7 +18,7 @@ describe("seriesAll", () => {
return sleep(1).then(() => 7);
},
])
).toStrictEqual([1, 2, 3, 4, 5, 6, 7]);
).toStrictEqual([1, 2, 3, 5, 6, 7]);
});

test("throw new Error", () => {
Expand Down
13 changes: 8 additions & 5 deletions src/helpers/seriesAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ import { isFunction, isPromise } from "../validators";
* ]); => [1, 2, 3, 4]
*/
export const seriesAll = async <T>(
series: (Promise<T> | Function)[]
series: (Promise<T> | (() => Promise<T>))[]
): Promise<T[]> => {
const results: T[] = [];
const results = [];
for (const fn of series) {
if (isPromise(fn)) results.push(await fn);
else if (isFunction(fn)) results.push(await fn());
// pure value? just return it
else results.push(fn);
else throw new Error("seriesAll: invalid type");
}
return results;

// TODO: "as T[];" fix TS error
// error TS2345: Argument of type '(() => Promise<T>) | Awaited<T>' is not assignable to parameter of type 'T'.
// 'T' could be instantiated with an arbitrary type which could be unrelated to '(() => Promise<T>) | Awaited<T>'.
return results as T[];
};

// TODO: rename to seriesAsync

0 comments on commit c9134ae

Please sign in to comment.