chrome extension send multiple data and wait all of them before continue

I’m making an extension where I need to pass several data from content.js to my background.js so I can use chrome.downloads.download API. This information contains filepath, URL to download, images.

Since I only can pass one item at a time and can’t pass HTMLCollections, I thought to develop a small if statement that can help me handle all of this.
The problem: I need to wait until I receive all messages so I can start the work.

With the following code, the file tries to download several times since I guess it runs while the for loop for the images is still running.

content.js

//download link
chrome.runtime.sendMessage({
  data: downloadlink,
  type: "m1"
})

//filepath
chrome.runtime.sendMessage({
  data: filepath,
  type: "m2"
})

//filename
chrome.runtime.sendMessage({
  data: nomeficheiro,
  type: "m3"
})

//for loop to send each image in HTMLCollection
for (const element of images) {
  chrome.runtime.sendMessage({
    data: element.src,
    type: "m4"
  })
}

background.js

const img = [] 
chrome.runtime.onMessage.addListener(
  function (request, sender, sendResponse) {
  // file url
  if (request.type == "m1") {
    downloadlink = request.data;
  // file path
  } else if (request.type == "m2") {
    filepath = request.data;
  // nome name
  } else if (request.type == "m3") {
    nomedoficheiro = request.data;
  // html collection images
  } else if (request.type == "m4") {
    img.push(request.data)
  }

  chrome.downloads.download({ 
    filename: filepath + "/" + nomedoficheiro, 
    url: downloadlink,
   });
  

});

How can I make all messages are sent, wait for all of them in background.js and only when I received all of them I would do the code to download the files? Sometimes it tries to download the same file several times (the number of times the images pushed into array), other times outputs error since filepath doesn’t exist, etc.

Thank you.