title: How to wait for multiple fetches to complete in JavaScript?
body: <p>I need to wait for two <code>fetch()</code> API calls and then to save data from each fetch to different window object. I found this article <a href="https://gomakethings.com/waiting-for-multiple-all-api-responses-to-complete-with-the-vanilla-js-promise.all-method/" rel="nofollow noreferrer">https://gomakethings.com/waiting-for-multiple-all-api-responses-to-complete-with-the-vanilla-js-promise.all-method/</a> and tried following code to wait for the API calls:</p>
  <pre><code>Promise.all([
      fetch('https://jsonplaceholder.typicode.com/todos/1'),
      fetch('https://jsonplaceholder.typicode.com/todos/2')
  ]).then(function (responses) {
      // Get a JSON object from each of the responses
      return Promise.all(responses.map(function (response) {
          return response.json();
      }));
  }).then(function (data) {
      // Log the data to the console
      // You would do something with both sets of data here
      console.log(data);
  }).catch(function (error) {
      // if there's an error, log it
      console.log(error);
  });
  </code></pre>
  <p>However when pasting this in browser console, the result is not being logged.</p>
  <p><strong>What is wrong with the code and how to wait for both to be resolved?</strong></p>