Skip to content

Commit

Permalink
refactor promise tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
yuheiy committed May 23, 2024
1 parent fce61e6 commit 34f6967
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 52 deletions.
11 changes: 5 additions & 6 deletions src/pages/[...slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import { getBlogDescription } from '../lib/get-blog-description';
import { renderDateToHtml } from '../lib/render-date-to-html';
export async function getStaticPaths() {
return getCollection('blog').then((entries) =>
entries.map((entry) => ({
params: { slug: entry.slug },
props: { entry },
})),
);
const entries = await getCollection('blog');
return entries.map((entry) => ({
params: { slug: entry.slug },
props: { entry },
}));
}
const { entry } = Astro.props;
Expand Down
29 changes: 14 additions & 15 deletions src/pages/blog.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ import { renderDateToHtml } from '../lib/render-date-to-html';
const title = 'ブログ';
const items = await getCollection('blog')
.then((entries) =>
entries
.toSorted((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.map(async (entry) => {
const description = await getBlogDescription(entry);
return {
title: entry.data.title,
link: `/${entry.slug}`,
meta: renderDateToHtml(entry.data.pubDate),
description,
};
}),
)
.then((promises) => Promise.all(promises));
const entries = await getCollection('blog');
const items = await Promise.all(
entries
.toSorted((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.map(async (entry) => {
const description = await getBlogDescription(entry);
return {
title: entry.data.title,
link: `/${entry.slug}`,
meta: renderDateToHtml(entry.data.pubDate),
description,
};
}),
);
---

<Layout title={title}>
Expand Down
29 changes: 14 additions & 15 deletions src/pages/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ import { getBlogDescription } from '../lib/get-blog-description';
export async function GET(context: APIContext) {
invariant(context.site);

const items = await getCollection('blog')
.then((entries) =>
entries
.toSorted((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.map(async (entry) => {
const description = await getBlogDescription(entry);
return {
link: `/${entry.slug}`,
title: entry.data.title,
pubDate: entry.data.pubDate,
description,
};
}),
)
.then((promises) => Promise.all(promises));
const entries = await getCollection('blog');
const items = await Promise.all(
entries
.toSorted((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.map(async (entry) => {
const description = await getBlogDescription(entry);
return {
link: `/${entry.slug}`,
title: entry.data.title,
pubDate: entry.data.pubDate,
description,
};
}),
);

return rss({
title: siteTitle,
Expand Down
31 changes: 15 additions & 16 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,21 @@ const projectItems = [
},
] satisfies DetailsListItem[];
const latestBlogItems = await getCollection('blog')
.then((entries) =>
entries
.toSorted((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.slice(0, 3)
.map(async (entry) => {
const description = await getBlogDescription(entry);
return {
title: entry.data.title,
link: `/${entry.slug}`,
meta: renderDateToHtml(entry.data.pubDate),
description,
};
}),
)
.then((promises) => Promise.all(promises));
const entries = await getCollection('blog');
const latestBlogItems = await Promise.all(
entries
.toSorted((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf())
.slice(0, 3)
.map(async (entry) => {
const description = await getBlogDescription(entry);
return {
title: entry.data.title,
link: `/${entry.slug}`,
meta: renderDateToHtml(entry.data.pubDate),
description,
};
}),
);
---

<Layout isHome description={siteDescription}>
Expand Down

0 comments on commit 34f6967

Please sign in to comment.