Variable's OnWrite Script

Hello everyone.  I was trying to accomplish something in a variable's OnWrite script, and noticed that (await ReadAsync()).Value wouldn't work.  The message I would get is:  "await expression is only allowed within an async function".  I think this message is referring to OnWrite as a "non-async function", and is not referring to the ReadAsync function.  Am I correct in that understanding?  It kinda makes sense that OnWrite needs to respond in a timely manner, but BOL and Script Help didn't really address this, the idea that an OnWrite script is of a different "type" or "class" than a Click script, and that certain coding approaches can't be used in OnWrite.  Is this also true of OnOpen, and "On" script types?  Any documentation/KBA reference anyone know of that details what can and can't be used where?

Thanks!

-Daniel

  • Better late than never, but OnWrite, OnOpen, and many of the other built-in script types are not built to be asynchronous. If you need to run anything like read/writeAsync but have it triggered by OnClick or some similar method, what you can do is call and define a separate async function within the code. For instance, the code below will take the name of my_module and return its value to be displayed in a MessageBox.

    Display.OnOpen(): void

    {

    const my_module = "XV-101";

    const run_result = runAsyncFunction(my_module);

    DL.MessageBoxAsync(run_result, "Value of my_module", Constants.MessageBoxButtonType.MB_OK, 30);

    async function runAsyncFunction(module_to_read: any) {

    return (await DLSYS.ReadAsync(module_to_read)).Value;

    }

    }

    I can't say for certain where BOL or some KBA would define what functions can and can't be used within these other built-in scripts, but the majority of asynchronous or timing-related functions can be implemented if they are deployed within a separate nested function as provided above.