Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

chrome.storageを同期的に扱う(ES7 async/await)

More than 3 years have passed since last update.

Chrome 53以上の環境で、起動オプションに下記のパラメータを追加することによって、async/awaitが使用可能となります。(2016/07/06現在)

--js-flags="--harmony-async-await"

以下、シンプルな実装例。

(async() => {
    await setLocalStorage({ aaa: 1, bbb: 2 });

    let aaa = await getLocalStorage("aaa");
    let bbb = await getLocalStorage("bbb");
    let all = await getLocalStorage();

    console.log(aaa);// 1
    console.log(bbb);// 2
    console.log(all);// {aaa: 1, bbb: 2}
})();

function setLocalStorage(obj) {
    return new Promise( (resolve) => {
        chrome.storage.local.set( obj, () => resolve() );
    });
}

function getLocalStorage(key = null) {
    return new Promise( (resolve) => {
        chrome.storage.local.get(key, (item) => {
            key ? resolve(item[key]) : resolve(item);
        });
    });
}
kururua
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away