Weird error messages related to forEach

Simple script to load images in a forEach loop. With the loop completely commented out, it works ok:

const imgURL = "path.to.img"
let widget = await createWidget(imgURL)
widget.presentLarge()

async function createWidget(imgURL) {
  let img
  let widget = new ListWidget()
//   [1,2].forEach(f => {
//    img = await loadImage(imgURL)
//    widget.addImage(img)
//   })
  img = await loadImage(imgURL)
  widget.addImage(img)
  return widget
}
  
  async function loadImage(url) {
    let req = new Request(url)
    return await req.loadImage()
}

Remove all comments and Scriptable complains

Unexpected identifier ‘loadImage’

Notice that there’s no error doing the exact same thing outside the loop. Remove only the two comments inside the forEach (leaving an empty function body), and the error message becomes

undefined is not an object (evaluating ‘new ListWidget()
[1,2].forEach’)

Both look like parser errors in the JavaScript engine to me. Or am I doing something wrong here?

Try this.

const imgURL = "https://scriptable.app/assets/appicon.png";
let widget = await createWidget(imgURL);
widget.presentLarge();

async function createWidget(imgURL)
{
	let img;
	let widget = new ListWidget();
	[1, 2].forEach(async f =>
	{
		//Effectively looping twice
		img = await customLoadImage(imgURL);
		widget.addImage(img);
	})
	//Do it a third time outside the loop
	img = await customLoadImage(imgURL);
	widget.addImage(img);
	
	return widget;
}

async function customLoadImage(url)
{
	let req = new Request(url);
	return await req.loadImage();
}

I assume that you are just testing things out with this as the structure seems “unusual”, but hopefully the amendments will help it make sense.

One amendment I made purely for my own sake and that was the renaming of the loadImage() function so I could distinguish it from where it was being used instead of the inbuilt one that is utilised within it.

Again, that did it. Basically you added the async in front of the anonymous function, right? That makes sense.
Thanks again.

I also needed to add in one of the semi colons during testing. I think it was the one on the previous line.