我如何使用 Observables 而不是 Promises?
How can I use Observables instead of Promises?
我有一个带有一些方法的服务,其中大部分都需要完成某个回调才能完成它的工作。使用 Promises,伪装起来很容易做到这一点:
ready = http.get(stuff); // Returns a promise, resolves after a while
methodOne() { // methods sometimes called before promise resolves
this.ready.then(_ => {
// doStuff
});
}
methodTwo() {
return this.ready.then(d => {
// doOtherStuff
});
}
基本上我需要做这些事情,只有当我确定服务已经准备好时。
我实际上只需要检查它是否准备就绪(methodOne
正在做什么,只是用 methodTwo
来说明,它也很容易做更多的事情)。
我想尝试全力投入 Observables,但对于这个特定案例,我发现很难与 Observables 的类似解决方案竞争。
Promises 会记住这个值并知道它是否得到解决。 Observable 稍微复杂一些,创建同样的流似乎很麻烦。我需要订阅 Observable 的任何东西,以便知道它何时准备就绪。有时该方法会提前调用 - 在 Observable 发出之前调用,有时会在 Observable 发出之后调用。
我现在有这个,但它似乎不起作用:
this.ready$ = someObservable // Will fire after a litle while but never finish - i only need the first to check though.
.publishReplay(1).refCount(); // Trying to replay if subscription comes after emit.
this.ready$.subscribe(_ => {
// This will be called
});
methodOne() {
this.ready$.subscribe(_ => {
// Not called
});
};
也许我误解了 publishReplay
和 refCount
的用法?
我想你要找的是AsyncSubject。它很好地模仿了承诺行为。这是描述:
The AsyncSubject is a variant where only the last value of the
Observable execution is sent to its observers, and only when the
execution completes.
以下是它在您的案例中的使用方法:
subject = new AsyncSubject();
ready = streamOfData(stuff).first().subscribe(subject);
methodOne() {
return this.subject.asObservable();
}
主题订阅由 first
运算符返回的基础可观察对象并等待它完成。它收集所有订阅者但不向他们发送任何值。一旦底层可观察对象完成,它就会记住该值并将其发送给收集的订阅者。所有新的未来订阅者将立即传递这个存储的解析值。
这是一个简单的示例,演示了您可以在可观察对象完成之前或之后进行订阅:
const subject = new AsyncSubject();
const o = subject.asObservable();
o.subscribe((v) => {
console.log(v);
});
interval(500).first().subscribe(subject);
setTimeout(() => {
o.subscribe((v) => {
console.log(v);
});
}, 2000);
我有一个带有一些方法的服务,其中大部分都需要完成某个回调才能完成它的工作。使用 Promises,伪装起来很容易做到这一点:
ready = http.get(stuff); // Returns a promise, resolves after a while
methodOne() { // methods sometimes called before promise resolves
this.ready.then(_ => {
// doStuff
});
}
methodTwo() {
return this.ready.then(d => {
// doOtherStuff
});
}
基本上我需要做这些事情,只有当我确定服务已经准备好时。
我实际上只需要检查它是否准备就绪(methodOne
正在做什么,只是用 methodTwo
来说明,它也很容易做更多的事情)。
我想尝试全力投入 Observables,但对于这个特定案例,我发现很难与 Observables 的类似解决方案竞争。
Promises 会记住这个值并知道它是否得到解决。 Observable 稍微复杂一些,创建同样的流似乎很麻烦。我需要订阅 Observable 的任何东西,以便知道它何时准备就绪。有时该方法会提前调用 - 在 Observable 发出之前调用,有时会在 Observable 发出之后调用。
我现在有这个,但它似乎不起作用:
this.ready$ = someObservable // Will fire after a litle while but never finish - i only need the first to check though.
.publishReplay(1).refCount(); // Trying to replay if subscription comes after emit.
this.ready$.subscribe(_ => {
// This will be called
});
methodOne() {
this.ready$.subscribe(_ => {
// Not called
});
};
也许我误解了 publishReplay
和 refCount
的用法?
我想你要找的是AsyncSubject。它很好地模仿了承诺行为。这是描述:
The AsyncSubject is a variant where only the last value of the Observable execution is sent to its observers, and only when the execution completes.
以下是它在您的案例中的使用方法:
subject = new AsyncSubject();
ready = streamOfData(stuff).first().subscribe(subject);
methodOne() {
return this.subject.asObservable();
}
主题订阅由 first
运算符返回的基础可观察对象并等待它完成。它收集所有订阅者但不向他们发送任何值。一旦底层可观察对象完成,它就会记住该值并将其发送给收集的订阅者。所有新的未来订阅者将立即传递这个存储的解析值。
这是一个简单的示例,演示了您可以在可观察对象完成之前或之后进行订阅:
const subject = new AsyncSubject();
const o = subject.asObservable();
o.subscribe((v) => {
console.log(v);
});
interval(500).first().subscribe(subject);
setTimeout(() => {
o.subscribe((v) => {
console.log(v);
});
}, 2000);