javascript - Typescript async/await with Observable Or Promise -
can use async await this? killing async/await here? code below working without error, know async/await works promise<t>. getitem observable. thanks.
roleservice.getitem({ page: p.pageindex + 1, pagesize: p.pagesize }) .takeuntil(this.unsubscribe$) .map((res: responsedto) => <dtomodel>res.contentbody) .do(async (dto: dtomodel) => await this.subject.next(dto.content)) // working wihtout error .subscribe(async (dto: dtomodel) => await (this.pager = dto.pager), //working without error (error: any) => observable.throw(error));
you can use async functions way have no effect in example. have understand async function returns promise:
const fn = async function() {}; fn() instanceof promise so if use in observable chain receive promise:
interval(500) .first() .map(async (v) => { return v; }) .subscribe((v) => { console.log(v instanceof promise); // logs `true` }); the way constructed example won't have effect on observables chain because output do operator ignored , there's no listener output subscribe callback. there's no reason use async there.
if wanted instead wait until async function resolves , results of function use mergemap operator:
interval(500) .first() .map(async (v) => { return v; }) .mergemap((p) => { return p; }) .subscribe((v) => { console.log(v); });
Comments
Post a Comment