Skip to content

Commit

Permalink
fix(forEach): fix a temporal dead zone issue in forEach. (#2474)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mprobst authored and benlesh committed Apr 3, 2017
1 parent 736d48d commit e9e9801
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ export class Observable<T> implements Subscribable<T> {
}

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

0 comments on commit e9e9801

Please sign in to comment.