tc39 / proposal-explicit-resource-management

ECMAScript Explicit Resource Management

Home Page:https://arai-a.github.io/ecma262-compare/?pr=3000

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any chance for an optional name binding?

itsMapleLeaf opened this issue · comments

I've found a few cases where I want to use using purely for executing a side effect within a scope. Here are some examples:

test("something", () => {
	using _ = mockTimers()
	// ideal: using mockTimers()

	doSomeTests()
})

function mockTimers() {
	vi.useFakeTimers()
	return {
		[Symbol.dispose]() {
			vi.useRealTimers()
		}
	}
}
{
	// atm, I have to use underscored names for unused variables
	using _someDependency = provideSomeDependency()
	using _someOtherDependency = provideSomeOtherDependency()

	// ideal:
	// using provideSomeDependency()
	// using provideSomeOtherDependency()

	doSomething()
}

function doSomething() {
	useSomeHelper()
}

function useSomeHelper() {
	const someDependency = useSomeDependency()
	const someOtherDependency = useSomeOtherDependency()
}

As far as I can remember there was using void = ... syntax for specifying a using without a name, but I don't know why it was removed.

Void bindings are in a separate proposal now

https://github.com/tc39/proposal-discard-binding

Yes, this achieved Stage 1 in the February 2024 plenary session.