3

I am exploring LWC with Flow. Basically I want the Submit button from the flow to trigger a validation on my app, but validation is done through window.postMessage, which the validation process should be async, and I want to wrap that into a Promise, so the validation actually waits for me to check everything in order to proceed. That is the idea, so I build a POC(Prof of Concept) to try to see if the Validate method that we have in this article works with async, and it seems that it doesn't, am I correct or I missed something?

  async firstMethod() {
    console.log("[firstMethod] Starts...");
    return new Promise((resolve) => {
      setTimeout(() => {
        console.log("[firstMethod] Executed...");
        resolve("1. Executed!");
      }, 5000);

      console.log("[firstMethod] Finished...");
    });
  }

  async mainMethod() {
    console.log("[mainMethod] Starts...");
    await this.firstMethod();
    console.log("[mainMethod] Finished...");
  }

  @api
  validate() {
    console.log(
      "[validate]Started... Test Rogerio Validate Method!!! ------------------- "
    );

// We should wait for this to finish, but that doesn't happen.
    await this.mainMethod();

    // Right now always return error but I will change that to get the response from mainMethod
    return { isValid: false, errorMessage: "Something Wrong" };

// If I set the return to return a promise that doesn't work either, it consider as successfull
    return new Promise((resolve) => {
      resolve({ isValid: false, errorMessage: "Something Wrong" });
    });
  }

I can see the "[firstMethod] Finished..." being executed but I don't see the ""[firstMethod] Executed..." being executed before the console.log("[mainMethod] Finished...");.

Another random question, is this code(@api validate) open source where I can look at the code? Try to look at the lwc but I couldn't find any info on that.

Thank you in advanced!

1 Answer 1

2

The flow runtime, as far as I'm aware, doesn't support asynchronous validation. LWC's documentation would definitely mention that it is possible if it were possible. You won't find the source that calls validate in your LWC, as it is called in the flow runtime. It is possible to find the source, but it will be hard to follow, as it is minified and slightly obfuscated.

1
  • Thanks for confirming! I took a different approach, I am triggering the next/finish button from the flow inside of my app. This way I control when to go to the next page or show an error message. Commented Jan 11 at 19:46

You must log in to answer this question.

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