Page 1 of 1

await is only valid in async functions

Posted: Wed Jan 12, 2022 4:42 pm
by congomonster
Hi,

need some help with my "newbie" question. I'm new to scripting with NodeJS and switch. I played around with it.
i copied the example from the documentation.

Code: Select all

async function jobArrived(s, flowElement, job) {

  const outConnections = flowElement.getOutConnections();
  for (const connection of outConnections) {
    if (job.isFile() && connection.getName() === 'Freigabe-Ordner') {
      await job.sendTo(connection); // here it is
    };
  };

};
But this give me the error: await is only valid in async functions and the top level bodies of modules
What I'm doing wrong here? Can someone explain?

thanks

Re: await is only valid in async functions

Posted: Wed Jan 12, 2022 6:23 pm
by freddyp
I see nothing wrong. Have you followed the correct procedure of creating the JavaScript code and linking to it from Switch Scripter and saving the script?

It gives you that error. Where? In the Switch Messages?

Re: await is only valid in async functions

Posted: Thu Jan 13, 2022 9:50 am
by congomonster
Yes, i think i linked it correctly. The errors shows up on switch internal webpage. And the file is routed to the problem folder. We have the Fall 2021 version installed by the way.

Re: await is only valid in async functions

Posted: Thu Jan 13, 2022 2:31 pm
by congomonster
I think it is working now. The SwitchScripter cashed the main.js fil under the roaming folder in Windows. I did a reconnect to the main.js file.

Re: await is only valid in async functions

Posted: Thu Jan 13, 2022 4:32 pm
by freddyp
Check the webinars under 3 and 4 on this page:
https://www.enfocus.com/en/learn/switch#scripting.

In the first of these two webinars you will hear multiple times: whenever you save the main.js script file, you also have to save the script in Switch Scripter for the reason that you mention: the script is cached.

In the second webinar it is explained how scripts can be developed in a different way by working with script folders (and in that case there is no need to save the script in Switch Scripter every time).