Trying to retrieve the last value from an observable. Here is an example of the code:
// RxJS v6+
import { lastValueFrom, Subject } from 'rxjs';
import { scan } from 'rxjs/operators';
async function main() {
const subject = new Subject();
const example = subject.pipe(
scan((acc, curr) => {
return Object.assign({}, acc, curr);
}, {}),
);
const subscribe = example.subscribe((val) =>
console.log('Accumulated object:', val),
);
subject.next({ name: 'Joe' });
subject.next({ age: 30 });
subject.next({ favoriteLanguage: 'JavaScript' });
console.log('+++++++++++++');
const resp = await lastValueFrom(example);
console.log(resp);
console.log('end');
}
main()
.catch(e => {
console.error(e);
})
.finally(async () => {
console.log('final');
});
The output displayed is:
➜ npns-service git:(mr/master/658) ✗ ts-node prisma/test.ts
Accumulated object: { name: 'Joe' }
Accumulated object: { name: 'Joe', age: 30 }
Accumulated object: { name: 'Joe', age: 30, favoriteLanguage: 'JavaScript' }
+++++++++++++
I am unable to see the response output message. How can I access the response value from the example Observable?