Nested API's in Angular
Hi Friends, This is Lakshman here.
As part of the Series Angular Best Practices
Today, We will Nested API's in Angular.
Pre-requisites:
The pre-requisites are
- Node.JS - Click Here
- Angular CLI - Click Here
- VS Code - Click Here
Nested API's
It is a Concept where we may call multiple API at one time or Chaining the API to Call one by one with passing data.
The mainly used functions to implement Nested API from rxjs library,
- forkJoin
- mergeMap
forkJoin
It used to call multiple APIs to fetch all data as an array at one time.
For example,
forkJoin([
return .helloService.getData(),
this.helloService.getStatus()
this.pipe(
])map(allResponses => {
return {: allResponses[allResponses.length - 2],
Data: allResponses[allResponses.length - 1]
Status;
}
}); )
mergeMap
It uses to chain the API by passing data from One API Response to another API.
For example,
.SearchUserByName(username).pipe(
return thismergeMap((responses) =>
zip(
of(responses),
.FetchFollowersByUser(responses.followers_url))
this
).pipe(
)map((allResponses) => {
= allResponses[allResponses.length - 1];
const responses 'UserInfo'] = allResponses[allResponses.length - 2];
responses[
;
return responses
}); )
That wraps up the Nested API's in Angular
You may also access to source code on GitHub by Click Here
On-road head, we will discuss a lot of things.
Looking forward to your comments and sharing your feedback.
Until that, I am Lakshman, Signing Off.
Comments
Post a Comment