asyncObservable
asyncObservable
Returns an observable which executes the given async work function and completes with the returned value. This is useful in Worker Controller methods which need to return an Observable but also want to work with async (Promise-returning) code.
Example
@Controller()
export class MyWorkerController {
@MessagePattern('test')
handleTest() {
return asyncObservable(async observer => {
const value = await this.connection.fetchSomething();
return value;
});
}
}
Signature
function asyncObservable<T>(work: (observer: Observer<T>) => Promise<T | void>): Observable<T>
Parameters
work
parameter
type:
(observer: Observer<T>) => Promise<T | void>