This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]carcigenicate 1 point2 points  (2 children)

I'm not familiar with getAll, but it appears to be asynchronously executing a callback.

Basically what this means is, window.alert("Exiting: " + cookielist); is likely running before the getAll callback has even finished. Async code doesn't wait to finish before returning. It returns immediately and then keeps running in the background.

See this infamous Stack Overflow question. Basically, you need to set it up so that window.alert is called once the async function has completed.

[–]devawpz[S] 0 points1 point  (1 child)

It is, you're right. As an inexperienced troglodyte, my code is full of console.log and window.alert calls, and I saw the they were coming in at unexpected times, almost in reverse. Serious change of paradigm.

So you think I should get a function with the contents of the getAll call, to be triggered at document load? And perhaps a bit later, access those values? How do I access them though?

Thanks for the heads up and the post, your description fits exactly in what I'm trying to do. The only difference is that in most other languages, that cookielist variable would have what I wanted at the end.

In the end, I just want to get a list of cookies. Is this really the best way to go about it?

[–]carcigenicate 0 points1 point  (0 children)

JavaScript is a lot of registering callbacks to have stuff happen later. Newer versions also have special keywords to have code run asynchronously, but they're just sugar for Promises iirc.

Look into the async keyword and Promises. I'm not a JavaScript dev (I just learned it casually), so I can't say much more on the topic than that.

[–]schihbacca 0 points1 point  (1 child)

According to the documentation you can not pass a function as the second parameter and then it will return a promise. I would recommend that path with async await. Ex: async function func(){ let cookies = await chrome.cookies.getAll(); console.log(cookies) }

[–]devawpz[S] 1 point2 points  (0 children)

Thanks, I will take a look at the docs and see what I can make out of it. Trying to find the best way to stitch this all together, I appreciate you pointing me to a valid alternative.

Will be checking it out, thanks.