This is a part of my chrome extension, but the part that I'm not understanding is from javascript.
let elementsArray = [
document.getElementById("main_keywords"),
document.getElementById("search_side"),
document.getElementById("news_emotion"),
document.getElementById("news_comments")
];
for (elem of elementsArray) {
var key = elem.getAttribute("id");
chrome.storage.sync.set({ key: false }, function() {
console.log(key + " saved : " + false);
chrome.storage.sync.get({ key }, function (result) {
console.log(key + " is " + result.key)
});
});
}
My intention was, for every element in elementsArray
, it should set {key:false}
to chrome.storage api using the id of the element. The result was that only the last element of elementsArray
, which is news_comments
, was saved 4 times. Console output is as below.
news_comments saved : false
news_comments saved : false
news_comments saved : false
news_comments saved : false
news_comments is false
news_comments is false
news_comments is false
news_comments is false
Chrome APIs are asynchronous but I don't have solid understanding on this concept although I'm not sure if this has to do with that.