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
import { pLimit } from "https://deno.land/x/p_limit@v1.0.0/mod.ts";
import { rateArticleRelevance } from "https://esm.town/v/iakovos/rateArticleRelevance";
export const processArticles = async (articleChunk, interests) => {
const concurrentRequestsLimit = 5;
const limit = pLimit(concurrentRequestsLimit);
const results = [];
for (const [index, article] of articleChunk.entries()) {
try {
const score = await limit(async () => {
const timeout = new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout")), 5000));
return await Promise.race([
rateArticleRelevance(interests, article),
timeout,
]);
});
results.push(score);
} catch (error) {
console.error(`Error processing article at index ${index}:`, error);
results.push(0);
}
}
return results;
};
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