-
-
Save OzanKurt/49ab3a59854e63bd3c173ad14e8b432c to your computer and use it in GitHub Desktop.
Using JavaScript Promises to stop render blocking with localStorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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