A
A
Alexey Bespalov2019-03-04 05:22:47
JavaScript
Alexey Bespalov, 2019-03-04 05:22:47

How to implement reading bookmarks after loading settings from storege?

Greetings!
I am writing an extension that displays bookmarks in chrome like opera.
Implemented specifying the ID of the root folder of bookmarks from which to read. I save the settings in chrome.storage.sync. And then I read them from there.
Faced a problem.
Since reading settings from storage is asynchronous, I need to start displaying bookmarks after reading the settings is completed. But as soon as I put the bookmark output code in the callback function of the read settings function, I get an error.

const getSettingsFromStorage= () => {
    chrome.storage.sync.get(["SETTINGS"], (result) => {
        const keys = Object.keys(result.SETTINGS)
        for (key of keys) {
            if (key != 'SETTINGS') {
                SETTINGS[key] = result.SETTINGS[key]
            }
        }
        loadedSettings = true
        getBookmarks()//эта функция выводит закладки
    })
}

Error handling response: TypeError: Error in invocation of bookmarks.getSubTree(string id, function callback): No matching signature.
    at getBookmarks (chrome-extension://einifcmggplbnlegodnhbibgbhcdjdeg/script.js:148:26)
    at chrome.storage.sync.get (chrome-extension://einifcmggplbnlegodnhbibgbhcdjdeg/settings.js:27:9)

And of course it works
getSettingsFromStorage()
getBookmarks()//вывод закладок на страницу

But I display bookmarks before reading the settings.
I also tried to describe promises, it's still an error.
And even if just calling the getBookmarks() function is inserted into setTimeout() , the same error occurs.
Script where settings are read
https://github.com/Bespalov-AV/bookmarks-in-tab-li...
Script where bookmarks are displayed
https://github.com/Bespalov-AV/bookmarks-in-tab-li. ..
Apparently I do not understand something.
Please tell me why this is happening and how can I get around this moment.
Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question