Asynchronous reading of data from the server storage API odnoklassniki. (java script phaser framework)

40 Views Asked by At

I work with java script framework Phaser. Does anyone have experience using the storage API https://apiok.ru/en/dev/methods/rest/storage/storage.get in classmates? The game originally stored everything in local storage, but during moderation they asked to store the progress of the players in the storage API OK. During the application of these methods, I encountered the problem of asynchronous execution, for example:

 createLevel(){
    this.scene.comboController.newLevel();
    let nextLevel = GameData.findFirstIncompleteLevel();
    GameData.getLevelByIndex(nextLevel, this.scene, this, this.onLevelReady);
}       

  static findFirstIncompleteLevel(){
        let key = GameData.getLocaleAwareKey(GameData.KEY_LAST_INCOMPLETE_LEVEL);
     

        getData(key , function(value) {

            if(value === "undefined"){

                console.log("findFirstIncompleteLevel(value === undefined)" + value);
                return JSON.parse(localStorage.getItem(key)) || 0;
            }
            else{

                console.log("findFirstIncompleteLevel(ELSE) =" + value);
                return JSON.parse(value) || 0;

            }

        });

    }




function getData (key, callback)   {
    FAPI.Client.call(
        {
            "method": "storage.get",
            "keys": key
        },
        function (status, data, error) {

            if (data) {
                  let tempValue = data.data[key];
               
            
                callback(data.data[key]);
            }
            else{
               
                callback(undefined);
            }

        }
    )

}

While the findFirstIncompleteLevel() method is waiting for a response from the server, the nextLevel variable is set to zero. I understand that the problem is in the asynchronous execution of requests to the server, but I can’t figure out how to make the code stop in this logic and wait until the response from the server arrives. Tell me how to implement it?

0

There are 0 best solutions below