From 6ebb034be574308219ee22689dbf190876db388e Mon Sep 17 00:00:00 2001 From: Martin Probst Date: Fri, 17 Mar 2017 11:52:18 +0100 Subject: [PATCH] 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 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. --- src/Observable.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Observable.ts b/src/Observable.ts index 0aa4b4cef3..87bf88cc63 100644 --- a/src/Observable.ts +++ b/src/Observable.ts @@ -139,7 +139,10 @@ export class Observable implements Subscribable { } return new PromiseCtor((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