javascript promise all a list of api calls. I need their results as well.
- User
javascript promise all a list of api calls. I need their results as well.
Assistant
Here is how you can make a list of API calls using Promise.all and get the results:
const apiCalls = [ fetch('https://api.com/users'), fetch('https://api.com/posts'), fetch('https://api.com/comments') ]; Promise.all(apiCalls) .then(responses => { // responses is an array of the response objects for each API call return Promise.all(responses.map(res => res.json())); }) .then(results => { // results is an array of the JSON results for each API call const users = results[0]; const posts = results[1]; const comments = results[2]; // Use the results... });Promise.all takes an array of promises as input, and returns a promise that resolves when all the input promises have resolved.
We map the array of responses to call .json() on each response, which returns a promise of the JSON result.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.