Skip to content

Commit 311e103

Browse files
authored
Revert "Revert "refactor: use Array.fromAsync()"" (#645)
1 parent 5fd5345 commit 311e103

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

tasks/db_dump.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ function replacer(_key: unknown, value: unknown) {
1515
return typeof value === "bigint" ? value.toString() : value;
1616
}
1717

18-
const iter = kv.list({ prefix: [] });
19-
const items = [];
20-
for await (const { key, value } of iter) items.push({ key, value });
18+
const items = await Array.fromAsync(
19+
kv.list({ prefix: [] }),
20+
({ key, value }) => ({ key, value }),
21+
);
2122
console.log(JSON.stringify(items, replacer, 2));
2223

2324
kv.close();

utils/db.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ export const kv = await Deno.openKv(path);
2828
* ```
2929
*/
3030
export async function collectValues<T>(iter: Deno.KvListIterator<T>) {
31-
const values = [];
32-
for await (const { value } of iter) values.push(value);
33-
return values;
31+
return await Array.fromAsync(iter, ({ value }) => value);
3432
}
3533

3634
// Item

utils/posts.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,11 @@ export async function getPost(slug: string): Promise<Post | null> {
6868
* ```
6969
*/
7070
export async function getPosts(): Promise<Post[]> {
71-
const files = Deno.readDir("./posts");
72-
const promises = [];
73-
for await (const file of files) {
74-
const slug = file.name.replace(".md", "");
75-
promises.push(getPost(slug));
76-
}
77-
const posts = await Promise.all(promises) as Post[];
78-
posts.sort((a, b) => b.publishedAt.getTime() - a.publishedAt.getTime());
79-
return posts;
71+
const posts = await Array.fromAsync(
72+
Deno.readDir("./posts"),
73+
async (file) => await getPost(file.name.replace(".md", "")),
74+
) as Post[];
75+
return posts.toSorted((a, b) =>
76+
b.publishedAt.getTime() - a.publishedAt.getTime()
77+
);
8078
}

0 commit comments

Comments
 (0)