private modifyAquireTokenFunction(): void { const originalAquireToken = AdalService.prototype.acquireToken; const that = this; AdalService.prototype.acquireToken = function (resource): Observable { var _this = this; // save outer this for inner function let errorMessage: string; return Observable.bindCallback(acquireTokenInternal, function (token: string) { if (!token && errorMessage) throw (errorMessage); return token; })(); function acquireTokenInternal(cb: any) { let s: string = null; _this.adalContext.acquireToken(resource, (error: string, tokenOut: string) => { if (error) { _this.adalContext.error('Error when acquiring token for resource: ' + resource, error); errorMessage = error; that.ngZone.run(() => { cb(null); }) } else { that.ngZone.run(() => { cb(tokenOut); s = tokenOut; }) } }); return s; } }; }