嗨,角度和字体大师,
https://medium.com/@benlesh/rxjs-observable-interop-with-promises-and-async-await-bebb05306875
在这个例子中,我面临两个问题:
-如何访问包含Observable的类的字段
这是一个示例/伪类
import { Observable, Subscription } from 'rxjs/Rx';
export class TestComponent {
private isRunning: boolean = false;
construct() {
this.createObservable()
.subscribe(
data => {
console.log("observable sent: " + data);
}
,error => {
console.log("observable had an error:");
console.log(error);
}
);
console.log("If the observable above is asynchronous this will be executed before the observable ends...");
//for some random event here terminate the observable
while(Math.floor(Math.random() * 10000) != 6073){ }
this.isRunning = false;
}
private createObservable():Observable<string> {
this.isRunning = true;
var obs: Observable<string> = Observable.defer(async function() {
while(this.isRunning) { //accessing this.isRunning here is an error
console.log("Observable is still running");
setTimeout(() => { }, 2000);
//main logic of the observable goes here...
}
//for some event I want to do a
//observer.next("test string");
//if an error occurs I want to do a
//observer.error()
//after some time... I will need to a
//observer.complete()
});
return obs;
}
}
接下来的问题是,这是不是一个适当的方法,使异步手动观察?
或者,我是否有正确的心态,认为每个可观察到的都是异步的。
因为当我在这里创建同一个类时,可观察对象/类基于“
import { Observable } from '../../node_modules/rxjs';
顺便说一下,我上面的例子是基于这个
import { Observable } from 'rxjs/Rx';
阿塔尼斯。