/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import {Component} from '@angular/core'; import {Observable} from 'rxjs/Observable'; import {Subscriber} from 'rxjs/Subscriber'; // #docregion AsyncPipePromise @Component({ selector: 'async-promise-pipe', template: `
promise|async: Wait for it... {{ greeting | async }}
` }) export class AsyncPromisePipeComponent { greeting: Promise|null = null; arrived: boolean = false; private resolve: Function|null = null; constructor() { this.reset(); } reset() { this.arrived = false; this.greeting = new Promise((resolve, reject) => { this.resolve = resolve; }); } clicked() { if (this.arrived) { this.reset(); } else { this.resolve !('hi there!'); this.arrived = true; } } } // #enddocregion // #docregion AsyncPipeObservable @Component({ selector: 'async-observable-pipe', template: '
observable|async: Time: {{ time | async }}
' }) export class AsyncObservablePipeComponent { time = new Observable((observer: Subscriber) => { setInterval(() => observer.next(new Date().toString()), 1000); }); } // #enddocregion // For some reason protractor hangs on setInterval. So we will run outside of angular zone so that // protractor will not see us. Also we want to have this outside the docregion so as not to confuse // the reader. function setInterval(fn: Function, delay: number) { const zone = Zone.current; let rootZone = zone; while (rootZone.parent) { rootZone = rootZone.parent; } rootZone.run( () => { window.setInterval(function() { zone.run(fn, this, arguments as any); }, delay); }); }