Background
I'm building an Angular application which lists companies that are registered at a given address.
To give some context...
Let's say I have x3 companies: Company A, Company B and Company C.
Company B is registered at the same address as Company A. Company C is not.
When I navigate to the app and filter on Company A, I expect to only see Company B in the list.
Problem
My issue is not that I couldn't get this working, it is simply too slow! I need to leverage multithreading/concurrency somehow.
In order to work out which companies are registered at the given address, I have to make several HTTP calls.
Before explaining the sequence in which I make the HTTP calls. Let me show you what the API looks like:
GET /api/companies/CompanyA/address
{
id: 1,
addressLine1: '123 Some street',
...
}
GET /api/companies/CompanyA/links
[
{id: 2, name: 'Company B'},
{id: 3, name: 'Company C'},
]
Right, here is the sequence:
- Get the address for Company A and store the ID
- Get the links for Company A
- Loop over each link 3a. Get the address of the link 3b. Check if the address ID matches Company A's address ID. If it does, store the link.
Current Implementation
const companyAId: number = 1;
const companyAAddress: Object = await this.httpService.getAddress(companyAId).toPromise();
const companyALinks: Object[] = await this.httpService.getLinks(companyAId).toPromise();
const companiesToShow: Object[] = [];
for (let link of links) {
const linkAddress: Object = await this.httpService.getAddress(link ['id']).toPromise();
if (linkAddress['id'] === companyAAddress['id']) companiesToShow.push(link );
}
There must be a more elegant / performant solution!
Any help will be greatly appreciated :)
Thanks,
Ben