0

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

2 Answers 2

4

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.

1
  • Ok, thank you. I will go with your approach. Dec 16, 2021 at 13:49
0

You can not prioritize wire methods in LWC (till date) but you can use the combination of wire and imperative to achieve what you're looking for.

Use 1 Wire method and add other as imperative Apex calls. More from here

1
  • thank you. I appreciate your answer. Dec 16, 2021 at 19:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .