Write a JS fetchAll function that concurrently fetches from an array of urls. The output…
- User
Write a JS fetchAll function that concurrently fetches from an array of urls. The output should be a result object of fulfilled array and rejected array.
- Assistant
Here's a JavaScript function called
fetchAllthat concurrently fetches data from an array of URLs and returns an object containing two arrays: one for the fulfilled responses and one for the rejected responses.async function fetchAll(urls) { const results = await Promise.allSettled(urls.map(url => fetch(url))); const fulfilled = []; const rejected = []; results.forEach(result => { if (result.status === 'fulfilled') { fulfilled.push(result.value); } else { rejected.push(result.reason); } }); return { fulfilled, rejected }; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.