I am tried to get key from chrome local storage in the typescript code(Node version: 16.x), this is the code to get the key from local storage:
const LocalStorage = {
readLocalStorage: async (key: string) => {
// https://stackoverflow.com/questions/59440008/how-to-wait-for-asynchronous-chrome-storage-local-get-to-finish-before-continu
return new Promise((resolve, reject) => {
chrome.storage.local.get([key], function (result) {
if (result[key] === undefined) {
resolve("");
} else {
resolve(result[key]);
}
});
});
},
}
export default LocalStorage
I want the get local storage to be sync, then I am using this code to get the value:
let accessToken:any = await LocalStorage.readLocalStorage(WheelGlobal.ACCESS_TOKEN_NAME);
the problem is that I have to add the async keywords in the function. In the outer level function, I also need to add await and async keywords. If not, the outer function will get the undefined result. Is it possible to just make the function did not add async keywords? what I want is just to make the get local storage sync, after get the key, the code continue, if not get the key, just wait. seems using the await keywords make some side effect.