Chrome.storage.Api doesn't return

206 views
Skip to first unread message

Divya Rijhwani

unread,
May 26, 2023, 5:19:35 AM5/26/23
to Chromium Extensions
Hello Team,

I precisely observed about chrome.storage.api where syntax is this..

chrome.storage.local.get(["key"]).then((result) => {
console.log("Value currently is " + result.key);
});

Here, return result.key doesn't work how to get my value rather than object in this API can you please help?


Message has been deleted

Browser Extenstion

unread,
May 26, 2023, 6:34:20 AM5/26/23
to Chromium Extensions, Divya Rijhwani
Your syntax is correct.

In additional you can use:
chrome.storage.local.get(["key"],  result => {

console.log("Value currently is " + result.key );
});

Do not forget to set the value before.

Divya Rijhwani

unread,
May 26, 2023, 6:51:21 AM5/26/23
to Chromium Extensions, Browser Extenstion, Divya Rijhwani
Hello Team,

My Set and Get both method works fine..

e.g
return chrome.storage.local.get(["tokenKey"]).then((result) => {
      console.log("Value currently is " + result.tokenKey);
var promise = Promise.resolve("This is a fulfilled promise");
promise = result.tokenKey;
console.log(promise);
return promise;
    });


    chrome.storage.local.set({ tokenKey: token }).then(() => {
      console.log("Value is set to " + token);
    });

I am getting console.log value is proper in both the methods But, as for function of get method it does return only result instead of result.key as mentioned in document. Please help me to make it return.

Patrick Kettner

unread,
May 26, 2023, 2:35:25 PM5/26/23
to Divya Rijhwani, Chromium Extensions, Browser Extenstion
Apologies, that is a mistake in our docs (I just sent a PR to update them). When you call set(), the promise you get does not give you access to the value you are setting. If this is something you would like to have, please do open a feature request on the bug tracker.  

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/83e34dad-b005-4f55-bbbe-8a5f75b88482n%40chromium.org.

wOxxOm

unread,
May 27, 2023, 12:37:04 AM5/27/23
to Chromium Extensions, Patrick Kettner, Chromium Extensions, Browser Extenstion, Divya Rijhwani
>  as for function of get method it does return only result instead of result.key as mentioned in document.

The method returns `result` object that looks like {['key']: value} and this is the correct behavior. The documentation doesn't contradict it, so I guess you're not reading it correctly. However, the code in the documentation is unnecessarily convoluted/outdated, which makes it confusing judging by the regularly posted questions about this API on stackoverflow.

Simply use this:

async doSomething() {
  const {token} = await chrome.storage.local.get('token');
  // use token here
}

async doSomething2() {
  const {token1, token2} = await chrome.storage.local.get(['token1', 'token2']);
  // use tokens here
}

Divya Rijhwani

unread,
May 28, 2023, 8:07:37 AM5/28/23
to Chromium Extensions, wOxxOm, Patrick Kettner, Chromium Extensions, Browser Extenstion, Divya Rijhwani
Hello wOxxOm,

The example given by you I have already tried and that gives the same Promise Object. But thank you for replying.

wOxxOm

unread,
May 28, 2023, 9:08:23 AM5/28/23
to Chromium Extensions, Divya Rijhwani, wOxxOm, Patrick Kettner, Chromium Extensions, Browser Extenstion
>  and that gives the same Promise Object

It is the correct behavior of the API and this is how you should use it. As my example shows the actual value from the storage is assigned to the variable `token`, `token1`, and `token2`. If you haven't used the await/async syntax before, it might be helpful to find a short tutorial to understand how it works.

wOxxOm

unread,
May 28, 2023, 9:16:37 AM5/28/23
to Chromium Extensions, wOxxOm, Divya Rijhwani, Patrick Kettner, Chromium Extensions, Browser Extenstion
In short, the return value of an `async` function is a Promise that resolves to the returned value, not the value itself.
To get the value the calling function should use promises properly e.g. inside another `async` function or via `then`.

async readStorage(key) {
  const res = await chrome.storage.local.get(key);
  return res[key];
}

async function usageExample1() {
  const val = await readStorage('token');
  console.log('val:', val);
  // use val here
}

function usageExample2() {
  return readStorage('token').then(val => {
    console.log('val:', val);
    // use val here
  });
}


Reply all
Reply to author
Forward
0 new messages