0

Without using jQuery or any other library (only vanilla js) I want the best way to execute a second method (of a class) after the first one has completed.

This is with respect to a react native expo app I'm creating. I want my second method to execute immediately after the first one

method 1

async load() {
    try {
        let mydata = await AsyncStorage.getItem("MyWeeklyTasks");

        if (mydata !== null) {
            this.state.products = JSON.parse(mydata);
            data2 = JSON.parse(mydata);
        } else {
            this.state.products = [];
        }
    } catch (err) {
        alert(err);
    }
}

method 2

setDataForDay(whichDay) {
    this.load();
    this.state.whichDay = whichDay;
    let filtered;
    if (whichDay == "all") {
        filtered = this.state.products;
        // filtered = data2;
    } else {
        filtered = this.state.products.filter((obj) => {
            return obj.day === whichDay;
        });
    }
    this.state.products = filtered;
    this.forceUpdateHandler();
}

This is my constructor

constructor(props) {
    super(props);
    this.load();
    this.state = {
        useThisId: "",
        inputTitle: "",
        inputDesc: "",
        products: data2,
        modalsetVisible: false,
        dataModalSetVisible: false,
        whichDay: "all",
        color: "white",
    };

    this.contentForModal = {
        content: "",
    };
    this.setDataForDay(this.state.whichDay);
    this.forceUpdateHandler = this.forceUpdateHandler.bind(this);
    alert("Click on the Day you wish to see");
}

In addition to the constructor I'm also calling load whenever the data is getting updated.

rishi
  • 652
  • 1
  • 6
  • 20
  • Where are you calling the methods? Or are you saying that you *always* want to run the second method at the end of the first one? Then just call it from there. – Bergi Dec 21 '20 at 10:35
  • What trouble do you have running them sequentially? You see that `load` returns a promise, right? – Bergi Dec 21 '20 at 10:35
  • @Bergi Yes both methods are working perfectly fine(I know it returns a promise). I'm calling the second method after the first one inside constructor, but if I call the second method inside the first one the code starts to hang. If I call it one after another inside constructor, it doesn't seem to get executed in that order. – rishi Dec 21 '20 at 11:44
  • @bergi I have a feeling the that `setdataforday` gets executed multiple times for some reason when I call it inside the first method`. Basically the issue is that whenever I open my app in one of the navigation pages, after refreshing it completely(closed fully), the data does not appear, I have to click on one of the buttons to make the data appear for some reason. But the data stays after it has loaded once, which makes me believe that my second method is not executing as soon as that page opens. – rishi Dec 21 '20 at 11:48
  • If you don't show that code we can't help you with it. Please [edit] your question to include a [mcve]. Btw, [don't call `load` from a constructor](https://stackoverflow.com/q/24398699/1048572). – Bergi Dec 21 '20 at 12:50
  • @Bergi I think I have provided the required code, I'm not sure what else I can show which will be relevant to the issue. I don't mind sharing my code but the project is rather large. Could you elaborate as to why I shouldn't call load from constructor, what is the alternative? – rishi Dec 21 '20 at 14:50
  • You need to show the part of the code where you call these two functions. All we know is "*I'm calling the second method after the first one inside constructor*", but we don't know how exactly you do that. Regarding not running asynchronous tasks from the constructor, see the linked answers – Bergi Dec 21 '20 at 15:13
  • @Bergi I have updated my question, please let me know if any additional code is required – rishi Dec 21 '20 at 16:07

0 Answers0