0

I have an array like this: json data

I'm trying to pass this variable into fetch request. I've try using JSON.stringify(data) but it return an empty array []

how can I fix this

This is my code so far:

const getCookies = (param) =>
  new Promise((resolve, reject) => {
    chrome.cookies.getAll(param, (cookies) => {
      resolve(cookies)
    });
  });

chrome.storage.sync.get(["isGet"], (item) => {
  try {
    var list = new Array();
    for (let i = 0; i < url.length; i++) {
      const param = {
        url: url[i],
      };
      getCookies(param).then((item) => {
        list[i] = item
      });
    }
    console.log(list);
    console.log(JSON.stringify(list))
    sendReq(list).then(() => {
      chrome.storage.sync.set({ isGet: true });
    });
  } catch (error) {
    chrome.storage.sync.set({ isGet: false });
    console.log(error);
  }
});
Hoang Dat
  • 1
  • 1
  • 2
  • getCookies is asynchronous so it completes long time after the encompassing loop, which means that list is empty when the loop finishes. You can use Promise.all + await, look for examples. – wOxxOm Oct 12 '22 at 18:34

0 Answers0