Execute one of Wire methods before other LWC

lightning-web-componentslwc-wire-adapterwire

I have five wire methods in my LWC.
And I want to execute one of them at first.
And only if this one is successfful – execute others.

Is there any way to tell my LWC Which wire method I want to execute as the first ?

Here some of my wire methods :

@wire(isUserLicenced, {userId : USER_ID})  // this one should be the first
isUserLicencedMethod({error,data}) {
    if (error) {
       console.error(error); 
    } else if (data) {
        console.log('isUserSetupLicenced'); 
    }
}

@wire(getPermissions)
getPermissionsData({ data, error }) {
    if(data) {
        console.log('getCustomPermissionsMapData'); 
    } else if (error) {
        console.error(error);
    }
}

@wire(getCashDrawerOpenReasons)
setCashDrawerOpenReasons({error,data}) {
    if(data) {
        console.log('setCashDrawerOpenReasons'); 
    } else if (error) {
        console.error(error);
    }
}

I saw this answer , However it did not help me!

Maybe someone has already done something similar ?

Any help will be appreciated.
thank you

Best Answer

Wire methods are all called at once, immediately after the first render cycle. You cannot control the order in which they will be called, nor can you prevent them from being called; wire methods are guaranteed to execute.

As such, you need imperative Apex calls:

  permissionsData
  cashDrawerOpenReasons
  @wire(isUserLicenced, { userId: USER_ID }) // this one should be the first
  isUserLicencedMethod({ error, data }) {
    if (error) {
      console.error(error);
    } else if (data) {
      console.log("isUserSetupLicenced");
      Promise.all([getPermissions(), getCashDrawerOpenReasons()]).then(
        (results) =>
          ([this.permissionsData, this.cashDrawerOpenReasons] = results)
      );
    }
  }

If you need to periodically refresh this data (it doesn't seem like it, but I thought I'd mention it), write a separate method that you can call to update the properties on demand.

Related Topic