W
W
WEWMEGADUV2016-06-16 11:13:00
Google
WEWMEGADUV, 2016-06-16 11:13:00

How to get a synchronous function from an asynchronous function?

in the plugin I need to get data from localstorage

function getAccessKeyId() {
    var a;
    chrome.storage.local.get(null, function (result) {
        a = result.AWSAccessKeyId

    })
    console.log(a)
    return AWS_ACCESS_KEY;
}

gives undefined in console

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AnjeyTsibylskij, 2016-06-16
@AnjeyTsibylskij

If it is possible to use ES7, then you can use async / await, if not, you can use promises

function getAccessKeyId() {
    return new Promise(function(resolve){
        chrome.storage.local.get(null, function (result) {
            resolve(result.AWSAccessKeyId);
        });
    });
};

getAccessKeyId().then(function(key){
    console.log(key);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question