When passing a Promise
to Subject
as a parameter:
const work = new Subject<{ id: number; dialogRef: Promise<typeof Dialog> }>();
I aim to utilize the instance inside the promise
at a later stage:
...
exhaustMap(({ id, dialogRef }) =>
http.get({ id }).pipe(tap(() => dialogRef.close()))
)
...
The issue arises because dialogRef
is a Promise
, requiring resolution before utilizing the instance function (close()
).
While one way to achieve this is using async await
, I am seeking an alternative to solve this using the "rxjs" approach.
I am in search of an operator or function that can resolve the promise and pass it to the exhaustMap
operator, similar to:
resolve(({ dialogRef }) => dialogRef),
exhaustMap(({ id, dialogRef }) => //<-- dialogRef is NOT promise. it's instance
http.get({ id }).pipe(tap(() => dialogRef.close()))
)
Is it possible to achieve this with rxjs?
import { of, Subject } from 'rxjs';
import { exhaustMap, tap } from 'rxjs/operators;
console.clear();
const http = {
get: ({ id }) => of(`data: ${id}`),
};
const Dialog = {
close: () => {
console.log('in close!');
},
};
const work = new Subject<{ id: number; dialogRef: Promise<typeof Dialog> }>();
work
.pipe(
exhaustMap(({ id, dialogRef }) =>
http.get({ id }).pipe(tap(async () => (await dialogRef).close()))
)
)
.subscribe((r) => {
console.log({ r });
});
work.next({ id: 1, dialogRef: Promise.resolve(Dialog) });