Im new to Async / Await and trying to make these 3 callouts to get Google Drive metadata in parallel using Node.js.
The 3 callouts are: rootFolder
, folders
, files
.
Since these call upon the getGdriveList
function which itself is an async function and includes an await
. The await
is needed as the API returns a page of records and I need to loop through all the pages to get the records. I use the await
to await the API response and add the data to an array. This process renders the code to run in series.
I am looking for help to refactor to make this parallel. Thanks in advance
const {google} = require('googleapis');
const gOAuth = require('./googleOAuth')
const aws = require('aws-sdk');
// initialize google oauth credentenatials
let readCredentials = gOAuth.readOauthDetails('credentials.json')
let authorized = gOAuth.authorize(readCredentials, getGfiles)
// get Google meta data on files and folders
function getGfiles(auth) {
let rootFolder = getGdriveList(auth, {corpora: 'user',
fields: 'files(name, parents)',
q: "'root' in parents and trashed = false and mimeType = 'application/vnd.google-apps.folder'"})
let folders = getGdriveList(auth, {corpora: 'user',
fields: 'files(id,name,parents), nextPageToken',
q: "trashed = false and mimeType = 'application/vnd.google-apps.folder'"})
let files = getGdriveList(auth, {corpora: 'user',
fields: 'files(id,name,parents, mimeType), nextPageToken',
q: "trashed = false and mimeType != 'application/vnd.google-apps.folder'"})
files.then(result => {console.log(result)})
}
const getGdriveList = async (auth, params) => {
let list = []
let nextPgToken
const drive = google.drive({version: 'v3', auth})
do {
let res = await drive.files.list(params)
list.push(...res.data.files)
nextPgToken = res.data.nextPageToken
params.pageToken = nextPgToken
}
while (nextPgToken)
return list
}