0

I'm trying to apply a Promise.all to an array of files. I want to iterate through the array and upload each file to firebase storage and then get the download link back. I get an error when I try the following

const uploadToFB = async () => {
try {
  await Promise.all(
    Array.from(uploadFiles!!).forEach((file) => {
      return new Promise((resolve) => {
        const storageRef = ref(storage, "images/" + uuid_v4() + ".jpg");
        uploadBytes(storageRef, file)
          .then((snapshot) => {
            return getDownloadURL(snapshot.ref);
          })
          .then((downloadUrl) => {
            resolve(downloadUrl);
          });
      });
    })
  ).then((res) => {
    console.log(res);
  });
} catch (error) {
  console.log(error);
 }
};

No overload matches this call. Overload 1 of 2, '(values: [] | readonly unknown[]): Promise<[] | unknown[]>', gave the following error.

Argument of type 'void' is not assignable to parameter of type '[] | readonly unknown[]'. Overload 2 of 2, '(values: Iterable): Promise<unknown[]>', gave the following error. Argument of type 'void' is not assignable to parameter of type 'Iterable'.

Captai-N
  • 1,124
  • 3
  • 15
  • 26
  • unbounded concurrency isn't necessarily a good idea when uploading files, bandwidth is typically limited and connection limits are sometimes enforced server side which would lead to errors when this code would attempt to upload too many files at the same time. It's also getting slower at some point. – zapl May 17 '23 at 18:55
  • What is the alternative if I want to allow users to upload multiple files to a cloud when clicking an upload button? – Captai-N May 17 '23 at 19:06
  • either a simple for loop that awaits each upload (and the whole thing wrapped in a promise if you need an immediate result) or somehow limiting how many uploads can happen at the same time, e.g. https://stackoverflow.com/q/40639432/995891 has some approaches. – zapl May 17 '23 at 19:14

0 Answers0