"if (request.method === "POST") { let { urls, chatid } = await request.json(); //…
- User
"if (request.method === "POST") { let { urls, chat_id } = await request.json(); // Destructure URLs and chat_id from the request body // let fetchPromises = urls.map(url => fetch(url, { method: 'HEAD' })); let fetchPromises = urls.map(url => fetch(url, { method: 'HEAD', cf: { cacheTtl: -1 } }));
codelet responses = await Promise.all(fetchPromises); let sizes = await Promise.all(responses.map(async (response, index) => { const size = parseInt(response.headers.get('Content-Length'), 10); return { url: urls[index], size }; })); // adding batch request start // const batchSize = 4; // Define how many URLs to process at a time // let sizes = await batchFetch(urls, batchSize); // Use the batchFetch function // console.log(sizes); // adding batch request end let validSizes = sizes.filter(item => !isNaN(item.size) && item.size < (50 * 1024 * 1024)); console.log(validSizes); if (validSizes.length === 0) { return new Response(JSON.stringify({ chat_id, error: "No files under 50MB found" }), { status: 404 }); } let closest = null closest = validSizes.reduce((prev, curr) => (50 * 1024 * 1024 - curr.size < 50 * 1024 * 1024 - prev.size) ? curr : prev); console.log("closest url is...") console.log(closest);"
instead of fetching all the urls at once, I want to fetch one after another in a loop. Please modify this code to do that
- Assistant
To fetch URLs one after another instead of all at once, you can use a loop to sequentially process each URL. Here's how you can modify your code to fetch URLs sequentially: