score:1

Accepted answer

apparently you are trying to save the state to the localstorage and load it after reloading the page

i think that you do not need to create a promise at the moment when you load the state from the localestorage

try to initialize the store as shown below

store.js

import {createstore} from "redux";
import appreducer from "./appreducer";

export const loadstate = () => {
    try {
        const state = localstorage.getitem('state');
        if (state === null) {
            return undefined;
        }
        return json.parse(state);
    } catch (err) {
        return undefined;
    }
};

export const savestate = (state) => {
    try {
        const st = json.stringify(state);
        localstorage.setitem('state', st);
    } catch (err) {

    }
};

const throttle = (func, limit) => {
    let lastfunc;
    let lastran;
    return function() {
        const context = this;
        const args = arguments;
        if (!lastran) {
            func.apply(context, args);
            lastran = date.now();
        } else {
            cleartimeout(lastfunc);
            lastfunc = settimeout(function() {
                if ((date.now() - lastran) >= limit) {
                    func.apply(context, args);
                    lastran = date.now()
                }
            }, limit - (date.now() - lastran))
        }
    }
};

const store = createstore(appreducer, loadstate());

store.subscribe(throttle(() => {
    // this callback is called on every state change.
    // here your current state is written to the localstorage
    savestate(store.getstate()); 
}, 2000));

export default store;

Related Query

More Query from same tag