Skip to content

Commit e9e9801

Browse files
mprobstbenlesh
authored andcommitted
fix(forEach): fix a temporal dead zone issue in forEach. (#2474)
According to the ES6 spec and the implementation in V8, accessing a lexically scoped construct like const or let its declaration has finished is a ReferenceError. Unlike var, it is not implicitly undefined in the entire function scope. Because the closure handed to subscribe is immediately invoked and accesses `subscription` before it is assigned, this causes a ReferenceError in compliant engines. The error is only triggered when running in plain ES6, i.e. without transpilation.
1 parent 736d48d commit e9e9801

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/Observable.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ export class Observable<T> implements Subscribable<T> {
139139
}
140140

141141
return new PromiseCtor<void>((resolve, reject) => {
142-
const subscription = this.subscribe((value) => {
142+
// Must be declared in a separate statement to avoid a RefernceError when
143+
// accessing subscription below in the closure due to Temporal Dead Zone.
144+
let subscription: Subscription;
145+
subscription = this.subscribe((value) => {
143146
if (subscription) {
144147
// if there is a subscription, then we can surmise
145148
// the next handling is asynchronous. Any errors thrown

0 commit comments

Comments
 (0)