hubotio / hubot

A customizable life embetterment robot.
https://hubotio.github.io/hubot/
MIT License
16.64k stars 3.75k forks source link

Using async calls in hubot scripts #1675

Closed johnseekins-pathccm closed 1 year ago

johnseekins-pathccm commented 1 year ago

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?

joeyguerra commented 1 year ago

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?

johnseekins-pathccm commented 1 year ago

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