Home > Net >  return response data from async call
return response data from async call

Time:08-30

I created this function to get list all my drives from GDrive.

    async getAllDrives(token) {
        let nextPageToken = ""
        let resultArray = []        
        const config= {
            headers: {
                Authorization: `Bearer ${token}`
            }
        };
        const bodyParams = {            
            pageSize: 2,
            fields: 'nextPageToken, drives(id, name)',
            q:`hidden=false`,
        };
        do {
            axios.get(
                `https://www.googleapis.com/drive/v3/drives`,
                config,
                bodyParams,
            ).then(result => {
                nextPageToken = result.data.nextPageToken;  
                resultArray.push(result.data.drives);
                resultArray = resultArray.flat();                
                console.log("result", resultArray);
            }).catch(error => {
                console.log(error);
                //res.send(error);
            });
        }while(nextPageToken);
        resultArray = resultArray.flat();
        resultArray.map(drive => {
            drive.isSharedDrive = true;
        return drive;
        });
        return JSON.stringify(resultArray);
        
    }

When I look in console.log

then(result => {
                nextPageToken = result.data.nextPageToken;  
                resultArray.push(result.data.drives);
                resultArray = resultArray.flat();                
                console.log("result", resultArray);

            })

I have the expected result,

result [
  {
    kind: 'drive#drive',
    id: '**',
    name: ' ★            
  • Related