Asynchronous 主题订阅,等待函数执行完成
我有个问题要问。我的情况是,我订阅了subject,我想在成功的可观察响应中运行一个函数。一旦我的函数(foo)完成(这个函数必须完成才能在成功响应中继续逻辑),我想将.next()方法传递给我的另一个主题。我的问题是,我不明白,我怎么能等到我的函数foo完成并返回值。如果我对另一个主题调用setTimeout.next()方法,那么一切都正常工作。我只是想避免设置超时。 我的代码的模式是:Asynchronous 主题订阅,等待函数执行完成,asynchronous,rxjs,Asynchronous,Rxjs,我有个问题要问。我的情况是,我订阅了subject,我想在成功的可观察响应中运行一个函数。一旦我的函数(foo)完成(这个函数必须完成才能在成功响应中继续逻辑),我想将.next()方法传递给我的另一个主题。我的问题是,我不明白,我怎么能等到我的函数foo完成并返回值。如果我对另一个主题调用setTimeout.next()方法,那么一切都正常工作。我只是想避免设置超时。 我的代码的模式是: this.customService.customSubject$.subscribe((respons
this.customService.customSubject$.subscribe((response) => {
this.someValueNeededInFoo = response;
this.foo(); //I need to wait for this function to be executed until I move on
this.customService2.anotherSubject$.next(true); //That will be great if I can wait for this as well
this.customService2.thirdSubject$.next(response); //This should invoke last
});
foo() {
//do some logic stuff
return value;
}
如果我在订阅中使用我的代码,如下所示:
this.customService.customSubject$.subscribe((response) => {
this.someValueNeededInFoo = response;
this.foo(); //I need to wait for this function to be executed until I move on
setTimeout(() => {
this.customService2.anotherSubject$.next(true); //That will be great if I can wait for this as well
this.customService2.thirdSubject$.next(response); //This should invoke last
}, 1000);
});
docIdSubject$ = new Subject<string>();
documentSubject$ = new Subject<Document>();
this.someService.id$.subscribe(
id => this.docIdSubject$.next(id)
);
this.docIdSubject$.subcribe(
docId => {
this.docService.getDocument(docId).subscribe(
document => this.documentSubject$.next(document)
);
);
);
那么一切都很好。我希望避免setTimeout,因为在我的情况下,它并不真正可靠。有谁能指导我,在这里我能做些什么,来进行一连串的函数调用吗?
谢谢 您的问题不在于可观察性,而在于
this.foo()
是一个异步方法,这意味着它返回一个承诺
您只需在调用foo()
后,将逻辑放入.then()
块中即可。这将确保在foo完成后执行:
this.customService.customSubject$.subscribe(response => {
this.someValueNeededInFoo = response;
this.foo().then(valFromFoo => {
this.customService2.anotherSubject$.next(true);
this.customService2.thirdSubject$.next(response);
})
});
您可以选择使用async/await
语法,而不是使用.then()
。以下代码相当于上面的示例:
this.customService.customSubject$.subscribe(async response => {
this.someValueNeededInFoo = response;
await this.foo();
this.customService2.anotherSubject$.next(true);
this.customService2.thirdSubject$.next(response);
})
});
我想提到的是,创建一组主题以从其他流中发出值通常是没有必要的。您通常能够定义彼此之间的流
而不是像这样做:
this.customService.customSubject$.subscribe((response) => {
this.someValueNeededInFoo = response;
this.foo(); //I need to wait for this function to be executed until I move on
setTimeout(() => {
this.customService2.anotherSubject$.next(true); //That will be great if I can wait for this as well
this.customService2.thirdSubject$.next(response); //This should invoke last
}, 1000);
});
docIdSubject$ = new Subject<string>();
documentSubject$ = new Subject<Document>();
this.someService.id$.subscribe(
id => this.docIdSubject$.next(id)
);
this.docIdSubject$.subcribe(
docId => {
this.docService.getDocument(docId).subscribe(
document => this.documentSubject$.next(document)
);
);
);
您可以使用可管道操作符以任何方式转换流。请注意,没有内部订阅。我发现,将所有数据转换逻辑声明性地放在流定义中(在.pipe()
中)要干净得多,在实际订阅中只剩下很少的工作要做
现在请注意,您只需执行
document$.subscribe()
您的问题并不在于可观察性,而是this.foo()
是一个异步方法,这意味着它返回一个承诺
您只需在调用foo()
后,将逻辑放入.then()
块中即可。这将确保在foo完成后执行:
this.customService.customSubject$.subscribe(response => {
this.someValueNeededInFoo = response;
this.foo().then(valFromFoo => {
this.customService2.anotherSubject$.next(true);
this.customService2.thirdSubject$.next(response);
})
});
您可以选择使用async/await
语法,而不是使用.then()
。以下代码相当于上面的示例:
this.customService.customSubject$.subscribe(async response => {
this.someValueNeededInFoo = response;
await this.foo();
this.customService2.anotherSubject$.next(true);
this.customService2.thirdSubject$.next(response);
})
});
我想提到的是,创建一组主题以从其他流中发出值通常是没有必要的。您通常能够定义彼此之间的流
而不是像这样做:
this.customService.customSubject$.subscribe((response) => {
this.someValueNeededInFoo = response;
this.foo(); //I need to wait for this function to be executed until I move on
setTimeout(() => {
this.customService2.anotherSubject$.next(true); //That will be great if I can wait for this as well
this.customService2.thirdSubject$.next(response); //This should invoke last
}, 1000);
});
docIdSubject$ = new Subject<string>();
documentSubject$ = new Subject<Document>();
this.someService.id$.subscribe(
id => this.docIdSubject$.next(id)
);
this.docIdSubject$.subcribe(
docId => {
this.docService.getDocument(docId).subscribe(
document => this.documentSubject$.next(document)
);
);
);
您可以使用可管道操作符以任何方式转换流。请注意,没有内部订阅。我发现,将所有数据转换逻辑声明性地放在流定义中(在.pipe()
中)要干净得多,在实际订阅中只剩下很少的工作要做
现在请注意,您只需执行
文档$.subscribe()
这有帮助吗?这有帮助吗?