1

As per my requirement, I need to send async request using the Angular 6 Httpclient.
Can anyone explain howe can send async request?

Loop calling in Component:

function in REST API service:

Right now, all request is going all together. But I want that after complete first request request 2nd should go and so all.

Please let me know is this possible in Angular 6?

Please try to provide me some good example.

this.pressArray.forEach(function(field_value, field_index){
  restAPI.CallAddPageBlock(formData, '', id).subscribe(response2 => {
    content_blocks.push(response2.id);
  });
});


CallAddPageBlock(formData, tickets='', id): Observable<any> {
  var full_url = this.baseUrlContent + 'Hub_PageBlock_C/assets/';
  if(id!=0){
    full_url = full_url+id
  }
  return this.http.post(full_url, formData, this.httpOptionsJson).pipe(); 
}

2

1 Answer 1

2

You could use async/await with Promise to stream line this. Convert the observable to a promise using toPromise()

async doSomethingAsync() {
  for(var item of this.pressArray) {
    let response2 = await restAPI.CallAddPageBlock(formData, '', id).toPromise();
    content_blocks.push(response2.id);
  }
}

There is no need to call pipe in CallAddPageBlock at the end there.

For more on async/await see also this excellent answer and scroll to the heading ES2017+: Promises with async/await.

Sign up to request clarification or add additional context in comments.

Comments

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.