Skip to content

mwmcode/async-web-storage

Repository files navigation

Async wrapper for web storage

npm install async-web-storage

Example

import { asyncLocalStorage, asyncSessionStorage } from 'async-web-storage';

async function saveLocally() {
  await asyncLocalStorage.setItem('foo', 'bar');
  const foo = await asyncLocalStorage.getItem('foo'); // bar

  const person = {
    name: 'mcha',
  };
  await asyncLocalStorage.setItem('user_1', person);
  const user = await asyncLocalStorage.getItem('user_1'); // {name: "mcha"}
}

All values are stored as JSON.stringifyed objects {[key]: value, createAt: Date.now()}

For example, the above values would exisit in local storage like this:

Key Value
foo "{\"foo\":\"bar\",\"createdAt\":1600516661351}"
user_1 "{\"name\":\"mcha\",\"createdAt\":1600516591899}"

To have access to the raw stored objects pass an option object {raw: true} as a 2nd argument .getItem

const rawStoredUser1 = await asyncLocalStorage.getItem('user_1', {raw: true});

console.log(rawStoredUser1); // {name: "mcha", createdAt: 1600516591899}

About

async web storage

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published