Home > Mobile >  Need to ensure a function returns data before i call next function
Need to ensure a function returns data before i call next function

Time:05-22

I have a function that retrieves some data from a local storage and then i need to pass it to another function. In my case this seems to be unreliable as i might call the second function before the data arrives and then i don't get the result as expected. Below is the code i am calling

const filterResult = this.gridFilterService.getPreviousFilter(this.gridGuid)
this.applyPreviousFilter(filterResult)

in here is the function which i call to get the data from Local Storage

  // Retrive the current stored Filters in Local store which matches gridGuid
    getPreviousFilter(gridGuid : string): IAgGridSearchFilterResult {
        const gridFilters = JSON.parse(localStorage.getItem('gridFilters'));
        if (gridFilters) {
            const activeFilters: any = gridFilters.find(x => x.gridGuid === gridGuid)
            if (activeFilters) {
                return activeFilters
            }
        }
    }

So what can i do to ensure i have a response before i call the

this.applyPreviousFilter(filterResult)

CodePudding user response:

Technically nothing!

Execution of code is line by line. So this.applyPreviousFilter(filterResult) will not get called until the previous line's execution has executed.

In your case, however, not all code paths in the getPreviousFilter function are returning a value. That is, return activeFilters, is called only in a double nested if statement.

You need to find a way to return data in corresponding else statements for both ifs. So that way, you are sure that getPreviousFilter will always return a value.

So you should have something like instead for the localStorage function:

// Retrieve the current stored Filters in Local store which matches gridGuid
getPreviousFilter(gridGuid: string): IAgGridSearchFilterResult {
  const gridFilters = JSON.parse(localStorage.getItem('gridFilters'));
  if (gridFilters) {
    const activeFilters: any = gridFilters.find((x) => x.gridGuid === gridGuid);
    if (activeFilters) {
      return activeFilters;
    } else {
      // uncomment the following line and return some default value
      // return defaultFilters;
    }
  } else {
    // uncomment the following line and return some default value
    // return defaultFilters;
  }
}

More Info

So what can i do to ensure i have a response before i call the

this.applyPreviousFilter(filterResult)

The only case where you would have needed to do something to ensure such would have been if there are asynchronous calls in the previous function. Then using async and await would have been okay.

CodePudding user response:

From your code snippet, everything seems to be sync code, so it already should be working sequentially (although as @obumuneme-nwabude mentioned in his answer, the getPreviousFilter method could not return the data).

You could change the strategy, making the getPreviousFilter method send the data through a subject, which would be used to trigger the applyPreviousFilter method when there's actual data.

But if your concern is more about the applyPreviousFilter method being called by itself in another part of the code in the near future, maybe you could turn it into a private method inside the service and create a public method to encapsulate both calls, which would assure they would always be run together.

Cheers!

  • Related