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
31
32
33
34
35
36
37
38
39
40
41
42
import { chunk } from "https://esm.town/v/iakovos/chunk";
import { processArticles } from "https://esm.town/v/iakovos/processArticles";
export const scoreAndCombineArticles = async (
articles: Article[],
interests: string,
): Promise<ScoredArticle[]> => {
const articlesPerChunk = 50;
const articleChunks = chunk(articles, articlesPerChunk);
let scoredArticles: ScoredArticle[] = [];
const processChunk = async (articleChunk: Article[]): Promise<ScoredArticle[]> => {
try {
const scores = await processArticles(articleChunk, interests);
return articleChunk.map((article, index) => ({ ...article, score: scores[index] }));
} catch (err) {
console.error("Failed to process chunk:", err);
return [];
}
};
const promises = articleChunks.map(processChunk);
const scoredArticleChunks = await Promise.all(promises);
for (const scoredChunk of scoredArticleChunks) {
scoredArticles.push(...scoredChunk);
}
return scoredArticles;
};
interface Article {
title: string;
description: string;
image: string | null;
pubDate: string;
type: string;
}
interface ScoredArticle extends Article {
score: number;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 30, 2023