From 34f69673085f80acefb47f4cfed9b37c11c53405 Mon Sep 17 00:00:00 2001 From: Yuhei Yasuda Date: Fri, 24 May 2024 02:45:49 +0900 Subject: [PATCH] refactor promise tasks --- src/pages/[...slug].astro | 11 +++++------ src/pages/blog.astro | 29 ++++++++++++++--------------- src/pages/feed.ts | 29 ++++++++++++++--------------- src/pages/index.astro | 31 +++++++++++++++---------------- 4 files changed, 48 insertions(+), 52 deletions(-) diff --git a/src/pages/[...slug].astro b/src/pages/[...slug].astro index 96a7915..4bc18f1 100644 --- a/src/pages/[...slug].astro +++ b/src/pages/[...slug].astro @@ -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; diff --git a/src/pages/blog.astro b/src/pages/blog.astro index d542b8b..d30d100 100644 --- a/src/pages/blog.astro +++ b/src/pages/blog.astro @@ -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, + }; + }), +); --- diff --git a/src/pages/feed.ts b/src/pages/feed.ts index e437265..62261ec 100644 --- a/src/pages/feed.ts +++ b/src/pages/feed.ts @@ -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, diff --git a/src/pages/index.astro b/src/pages/index.astro index 8dad143..1334338 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -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, + }; + }), +); ---