1

I am new at JS programming but I am trying to write an extension that gets cookies for specific domains and processes them when a button is clicked. I am trying to use promises/await but the code does not seem to wait till the promise is resolved.


async function getCookies(domain) {
    let promise = new Promise((resolve, reject) => {
        chrome.cookies.getAll({"domain": domain}, function(cookies) {
            resolve(cookies);
        });
    });
    let cookies = await promise
    return cookies;
}


document.addEventListener('DOMContentLoaded', function() {
    var link = document.getElementById('btnremove');  
    link.addEventListener('click', function() {
        var domains = ["www.horizonhobby.com", "yolo.com", "rcgroups.com", "yotpo.com"];
        domains.forEach(domain => {
            let cookies = getCookies(domain);
            console.log("Got " + cookies.length + " cookies for " +  domain);
      });
    });
});

My console log message gets called before the cookies are returned from the API. Looking in DEVTools cookies is defined as:

cookies: Promise proto: Promise [[PromiseState]]: "pending" [[PromiseResult]]: undefined

Ted B
  • 11
  • 1
  • `let cookies = getCookies(domain);` ... you'll need to `await` or `then` since `getCookies` returns a promise. If you want to wait until all cookies have been retrieved, try `Promise.all` on the array of promises. – ggorlen Feb 19 '21 at 03:07
  • ```let cookies = await promise``` shouldn't this wait till the promise is either resolved or rejected? Can't use await on ```let cookies = getCookies(domain);``` since this is not in an async function – Ted B Feb 19 '21 at 03:13
  • Yes, you'll need to add `async` to the `forEach` callback if you prefer to use `await`. Or use `getCookies(domain).then(cookies => console.log(cookies))`. But what you're trying to do here is impossible: async code cannot be converted into sync code. The sync code will execute first and the promise is guaranteed to be in a pending state. The entire sync call stack must empty before promises get a chance to resolve. Check out the canonical on [using promises](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ggorlen Feb 19 '21 at 03:25
  • Adding the async worked to the forEach worked. Thank you. I am complete newb when it comes to async programming and I understand the benefit of it, but in my case execution of my program could not go forward until the cookies were retrieved. I still don't understand why it needed another await. I would appreciate it if you had the time and could explain it a little further. If not I get it. Thanks again! – Ted B Feb 19 '21 at 04:33
  • Great! Happy to help--don't worry, promises were confusing for me for a long time. Read the link above, it's a fantastic tutorial. You may be confused because you might think `let cookies = await promise` "resolves" the promise (it does), but the problem is all functions marked `async` implicitly return a promise, so it just gets packed into another promise. You might as well have used `return promise` here. – ggorlen Feb 19 '21 at 04:51

0 Answers0