Skip to content
Advertisement

How to upload multiple files to GoogleDrive API using for/loop (forEach)

I use the Google Drive API to upload multiple files. I faced with a problem running out of RAM while uploading multiples files. I try to use forEach (for loop) for my code to avoid uploading multiple files at the same time, but It doesn’t work the way I expected. It always loop through the entire of list files and upload the same time.

I try to use async/await syntax to block the loop but It didn’t work the way I expected. Here is my code:

JavaScript

I just want to ask why the loop does not upload single files each?

Advertisement

Answer

I try to use forEach (for loop) for my code to avoid uploading multiple files at the same time

You can’t, the process is entirely asynchronous. You passed a callback as an argument to the function drive.files.create.

By the way, if you want to use async/await, you should wrap your function into a promisified one.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement