title: How to properly make a promise.all function with a .map?
body: <p>I attempted to ask a simplified version of this <a href="https://stackoverflow.com/questions/71401730/how-to-correctly-pass-parameters-through-nested-functions-in-for-loop/71404176#71404176">here</a> but I realized that it probably doesn't contain enough information. So unfortunately I'm just going to post the whole thing and hope that it doesn't offend anyone.</p>
  <p>Basically I have 4 functions, with their stated purposes below:</p>
  <p>1.<code>InitializeDrive()</code> takes a user email and uses Google's JWT user impersonation method to return the appropriate authorization for that user.</p>
  <p>2.<code>listfiles()</code> calls on <code>InitializeDrive()</code> for authorization and retrieves a list of the documents associated with the associated user auth.</p>
  <p>3.<code>singleUserData()</code> expects a list of the files such as that from <code>listfiles()</code> and refines them.</p>
  <p>4.<code>all_user_data()</code> is intended to be the Promise.all async function that combines all of the aforementioned functions, and maps to an array of users <code>getListUsers()</code>, creating a <strong>master array</strong> containing all of the refined file data for each user.</p>
  <p>Basically my poorly formed and ignorant question is 'how can make the <code>all_user_data()</code> in such a way that it will return the aforementioned <strong>master array</strong>?'. I am relatively new to async programming, and have a persistent problem getting confused with nested functions.</p>
  <pre><code>// loads credentials from .env file
  require('dotenv').config();
  const util = require('util');
  const { google } = require('googleapis');
  const { logger } = require('handlebars');
  const { getListUsers } = require('./get_users');
  const target_users = getListUsers();
  
  function initializeDrive(version, user) {
  return new Promise((resolve, reject) =&gt; {
  const client_email = process.env.GOOGLE_CLIENT_EMAIL;
  console.log(client_email);
  // add some necessary escaping so to avoid errors when parsing the private key.
  const private_key = process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n');
  // impersonate an account with rights to create team drives
  const emailToImpersonate = user;
  const jwtClient = new google.auth.JWT(
    client_email,
    null,
    private_key,
    ['https://www.googleapis.com/auth/drive'],
    emailToImpersonate,
  );
  return google.drive({
    version: version,
    auth: jwtClient,
   });
   });
  }
  
  const listfiles = async (pagetokenObj, user) =&gt; {
  let pageToken = '';
  let version = 'v3';
  if (pagetokenObj !== undefined) {
  pageToken = pagetokenObj.pageToken;
  }
  const drive = await initializeDrive(version, user);
  //const drive = initializeDrive(version,user);
  return new Promise((resolve, reject) =&gt; {
  drive.files.list(
    {
      pageSize: 100,
      fields:
        'nextPageToken, files(id, name, owners(emailAddress), 
    sharingUser(emailAddress), permissions)',
      ...(pageToken ? { pageToken } : {}),
    },
    function (err, { data: { nextPageToken = '', files = [] } = {} }) {
      if (err) {
        return reject(err);
      }
      if (!nextPageToken) {
        return resolve(files);
      }
      // if page token is present we'll recursively call ourselves until
      // we have a complete file list.
      return listfiles({ pageToken: nextPageToken }).then((otherfiles) =&gt; {
        resolve(files.concat(otherfiles));
      });
      },
     );
   });
  };
  
  //Function returns formatted
  const singleUserData = async (files) =&gt; {
  return new Promise((resolve, reject) =&gt; {
  const single_user_json = await listfiles();
  const res = single_user_json
    .filter((doc) =&gt; Boolean(doc.permissions))
    .map((doc) =&gt; {
      return {
        id: doc.id,
        sharedWith: doc.permissions
          .filter((permission) =&gt; permission.type === 'user')
          .map((permission) =&gt; {
            return {
              emailAddress: permission.emailAddress,
              role: permission.role || null,
            };
          }), // convert the perm object into a string (email address)
      };
    });
  // this is how you get nicer console.logs instead of just [Object] and [Array] BS
  // https://stackoverflow.com/a/10729284/9200245
  console.log(util.inspect(res, { showHidden: false, depth: null, colors: true }));
  if (err) {
      return reject(err);
    }
    if (!nextPageToken) {
      return resolve(files);
  };
  })
  };
  
  const all_user_data = async() =&gt; {
  const users = await getListUsers();
  const pagetokenObj = &quot;{ pageToken = '' } = {}&quot;;
  Promise.all(
  users.map(async (pagetokenObj,user) =&gt; {
    const files = await listfiles(pagetokenObj, user);
    console.log(files);
  
    const singleUserData = await singleUserData(files)
    console.log(singleUserData);
    }),
   );
   console.log(users)
   }
  
  
  //returnJSON();
  //getListUsers();
  //singleUserData();
  all_user_data();
  </code></pre>