Skip to content

Instantly share code, notes, and snippets.

@OzanKurt
Forked from JustSteveKing/localStorage.async.js
Created January 13, 2023 01:56
Show Gist options
  • Save OzanKurt/49ab3a59854e63bd3c173ad14e8b432c to your computer and use it in GitHub Desktop.
Save OzanKurt/49ab3a59854e63bd3c173ad14e8b432c to your computer and use it in GitHub Desktop.
Using JavaScript Promises to stop render blocking with localStorage
class Storage {
get(item) {
const request = new Promise((resolve, reject) => {
let data = localStorage.getItem(item);
console.log(`Start of promise`);
(data) ? resolve(data) : reject();
});
request.then((data) => {
console.log(`promise resolved`);
console.log(JSON.parse(data));
});
request.catch((error) => {
console.log(`Promise rejected`);
console.log(error);
});
}
set(name, data) {
localStorage.setItem(name, JSON.stringify(data));
}
}
const Store = new Storage;
let test = {
name: 'test',
token: '12lj33i237yqekjbfq.183rkjew'
}
Store.set('test', test);
Store.get('test');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment