1

How do you get the information for the Chrome extension by using async and await?

[chrome.instanceID.getID]
[chrome.storage.sync.get]

We tried this code:

async function test()
{
let _r = await chrome.instanceID.getID();
return _r;
}
let _pc_id = test();

but _pc_id returns a promise. We find no way to get the value in it. How should we do this?

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
user1566268
  • 75
  • 11

1 Answers1

0

You can get the instanceID like this, but can't store it in a variable to use it out of scope of the promise, AFAIK. You may want to read this: saving data from promise in a variable

If you want to use the returned value of Promise you need to do it in the promise or in .then()s after the promise, at least that is how I do it.

chrome.instanceID.getID example:

chrome.instanceID.getID((instance_id) => {
    console.log(instance_id);
    // Execute your other related code here
});

or

var promise = chrome.instanceID.getID();
promise.then((instance_id) => {
    console.log(instance_id);
    // Execute your other related code here
});

or

chrome.instanceID.getID()
.then((instance_id) => {
    console.log(instance_id);
    // Execute your other related code here
});

chrome.storage.sync.get example:

chrome.storage.sync.get('myKey', function(items) {
    var key = items.myKey;
    // Below code is an example, change it to your needs
    if (key) {
        console.log(key)
    } else {
        key = createKey();    // a custom function that generates keys
        chrome.storage.sync.set({myKey: key}, function () {
            console.log(key);
        });
    }
};
bhdrozgn
  • 167
  • 10