2

Can i use the async with await like this? Am i killing the async/await here? The code below is working without error, I know that the async/await only works with Promise<T>. My getItem is an 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));
3
  • what are you trying to do? Commented Jul 25, 2017 at 11:29
  • You can do that but you shouldn't Commented Jul 25, 2017 at 11:36
  • Why are you trying to await a (synchronous!) assignment? Commented Jul 25, 2017 at 11:37

1 Answer 1

2

You can use the async functions the way you do but it will have no effect in your example. You have to understand that async function returns a promise:

const fn = async function() {};
fn() instanceof Promise

So if you use it in the observable chain you will receive a promise:

interval(500)
  .first()
  .map(async (v) => {
    return v;
  })
  .subscribe((v) => {
    console.log(v instanceof Promise); // logs `true`
  });

The way you constructed your example it won't have any effect on the observables chain because output from do operator is ignored and there's no listener for the output from the subscribe callback. So basically there's no reason to use async there.

If you wanted instead to wait until async function resolves and get the results of that function use mergeMap operator:

interval(500)
  .first()
  .map(async (v) => {
    return v;
  })
  .mergeMap((p) => {
    return p;
  })
  .subscribe((v) => {
    console.log(v);
  });
Sign up to request clarification or add additional context in comments.

7 Comments

So how do i wait this do((dto: DtoModel) => this.subject.next(dto.content)) before subscribe statement? I have TODO with that subject. I don't know how much time it takes to complete so i can't chain with .delay(...) Is there a better way to wait until the first statement complete before next? Thanks.
why do you want to wait for it? does it return any value?
It does not return anything. The subject send the new value from do(...) where the subject is subscribed to angular component for data changes on template. So i have to wait until angular scan and complete from changeDetecoterRef. My component change detection strategy is in default. If i chain with .delay(..) then the second statement value is bound to angular but if i remove, the second value is not applied.
@Robin, sorry, I don't understand. What is unclear about my answer?
@Maximus I understood your answer's example above, but my problem isn't resolved. I mean how to tell the angular change detection that foo[] being process while bar[] is updated and rendering in view? Wait till foo[] & bar[] to complete and then start rendering to the view/component etc. Like if i have <div *ngFor="let f of foo"> & <div *ngIf="bar.length> 0 && getFromBar(f.id)"> so how do i chain and do synchronously? Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.