Neither chrome.storage.local.get(...)
nor browser.storage.local.get(...)
return any data.
Both accept a callback as the last parameter and will asynchronously pass their data as an argument to that callback.
With browser.storage.local.get(...)
you can omit the callback because it also returns a Promise. Promises are special ES6 language objects. Semantically they promise that some value will be avaiable in the future. There are two ways to get that future value: calling .then(...)
on the promise and using the await
keyword.
The latter only works form Firefox 52 onwards (and in current Chrome/Opera/Edge versions too), but allows for a semantic that looks as if the function actually returned a value:
const value = await browser.storage.local.get(...);
You can, however, only use await
in functions that are themselfs marked as async
, which makes them return a Promise to their actual return value (so if you call them and need that value, you’ll have to use an await
in an async function again:
async function getRandomStorageValueAndAdd2() {
const key = Math.random();
const value = await browser.storage.local.get(key);
return value + 2;
}
(async function main() {
const value = getRandomStorageValueAndAdd2();
console.log('useless value:', value);
})();