我有這個函式呼叫另一個微服務函式,我想在訂閱中添加所有元素后回傳標簽。
這是在 Nestjs 中使用微服務完成的。
因為現在這只是回傳空陣列,但我希望它與元素一起回傳。
有誰知道修復?謝謝
private microservicesOptions: ClientOptions = {
transport: Transport.TCP,
options: {
host: host,
port: 3006
}
}
private filterProxy: ClientProxy;
constructor() {
this.filterProxy = ClientProxyFactory.create(this.microservicesOptions);
}
async getAllTags() {
let tags = []
this.postMicroserviceProxy.send<any>("get_posts", "").subscribe(response => {
response.forEach(element => {
element.tags.forEach(tag => {
tags.push(tag)
})
});
});
return tags;
}
uj5u.com熱心網友回復:
你不需要async方法。async僅當您也使用時才有意義await,并且await在這里沒有必要。
只需回傳一個常規的承諾:
getAllTags() {
return this.postMicroserviceProxy
.send<any>("get_posts", "")
.toPromise()
.then(response => response.flatMap(element => element.tags));
}
因為它回傳一個承諾,你仍然可以使用await這個函式,但是:
async test() {
const tags = await foo.getAllTags();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/406950.html
標籤:
