Skip to main content

Nested Array Iteration Breaks Closure Variables

By Web Design2 min read

While working on my Big Sexy Poems app (GitHub), I ran into another wild bug in Adobe ColdFusion (see previous post). I don’t exactly know how to articulate this one; but, it seems that if I perform nested array iteration within the context of an asynchronous array iteration, closed-over variables are no longer available within a closure scope.

This breaks in both Adobe ColdFusion 2021 and 2025 with the same error. Here’s my reproduction code:



	// ASYNCHRONOUS array iteration.
	arrayEach(
		[ "" ],
		() => foo( 999 ),
		true
	);

	public array function foo( required numeric flag ) {

		return arrayFilter(
			bar(),
			( result ) => flag // BUG: This closed-over variable is UNDEFINED!
		);

	}

	public array function bar() {

		return arrayFilter( [ "" ], ( element ) => true );

	}


There are three layers of iteration happening here:

  1. The asynchronous iteration that kicks of the demo.
  2. The arrayFilter() in foo().
  3. The arrayFilter() in bar().

When I run this in Adobe ColdFusin 2021/2025, it breaks with the following error:

coldfusion.runtime.UndefinedVariableException:

Variable FLAG is undefined.

I can remove the error by either:

  • Changing from asynchronous to synchronous iteration in the first arrayEach() call. But, this only fixes the issue in ACF 2025 – the error persists in ACF 2021.

  • Removing the arrayFilter() in bar(). For example, if I just return the array literal, [""], which is what the deeply nested array iteration was creating, it fixes the issue in both ACF 2021 and ACF 2025.

This one is just strange! I wish I could explain it better. It took me several hours to narrow down on the reproduction case because this only popped up in my ColdFusion application when I had several nested calls to a series of ColdFusion components.

Want to use code from this post?
Check out the license.


https://bennadel.com/4831


Source link