15
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

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);
        });
    });
}
15
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?