Skip to content

Commit 7ea87ed

Browse files
committed
fix(forEach): fix a temporal dead zone issue in forEach.
According to the ES6 spec and the implementation in V8, accessing a lexically scoped construct like const or let before it is assigned is a ReferenceError. Unlike var, it is not implicitly undefined. 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 e2696db commit 7ea87ed

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

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 explicitly assigned to avoid RefernceError when accessing
143+
// subscription below in the closure due to Temporal Dead Zone.
144+
let subscription: Subscription = undefined;
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)