hubotio / hubot

A customizable life embetterment robot.

Home Page:https://hubotio.github.io/hubot/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using async calls in hubot scripts

johnseekins-pathccm opened this issue · comments

I'm trying to add a nice little script so Hubot can show us inventory of some assets. While the general case makes sense, I can't seem to leverage async/await correctly:

bot-scripts/aws.mjs: SyntaxError: Unexpected reserved word\n  at ESMLoader.moduleStrategy (node:internal/modules/esm/translators:119:18)\n  at ESMLoader.moduleProvider (node:internal/modules/esm/loader:468:14)\n  at link (node:internal/modules/esm/module_job:68:21)\n"}
file:///Users/johnseekins/gitroot/hubot/custom-hubot-scripts/aws.mjs:41
      const response = await ecsClient.send(command);
                       ^^^^^

[SyntaxError: Unexpected reserved word
  at ESMLoader.moduleStrategy (node:internal/modules/esm/translators:119:18)
  at ESMLoader.moduleProvider (node:internal/modules/esm/loader:468:14)
  at link (node:internal/modules/esm/module_job:68:21)
]

Node.js v18.18.0

Any tips on how to properly use an async function in Hubot?

It's hard to tell without seeing all of the code, but the one thought that comes to mind is that const response = await ecsClient.send(command); is not within an async function, it's "just out in the main script".

What I mean is I would expect the code to look something like the following:

export default async robot => {
  robot.respond(/ecs command (.*)/, async res => {
    ....
    const response await ecsClient.send(command);
  })
}

See how the robot.respond 2nd argument is an async function and the await call is being done inside that scope? Where is ecsClient.send(command) being executed in the scope of the script? Is the top level scope? or inside the scope of an async function?

What you showed me solved my problem. I frequently struggle with JS syntax. Thanks for the help!