Complete the code to define the aggregator service endpoint.
app.get('/aggregate', async (req, res) => { const data = await [1](); res.send(data); });
The aggregator service calls fetchDataFromServices to gather data from multiple microservices.
Complete the code to aggregate responses from two microservices.
async function fetchDataFromServices() {
const [data1, data2] = await Promise.all([
fetchServiceA(),
[1]()
]);
return { data1, data2 };
}The aggregator calls fetchServiceA and fetchServiceB concurrently to get data from both services.
Fix the error in the aggregation function to handle failed service calls.
async function fetchDataFromServices() {
try {
const data = await Promise.all([
fetchServiceA(),
fetchServiceB()
]);
return data;
} catch ([1]) {
return { error: 'Service call failed' };
}
}The catch block needs a variable to capture the error; e is a common and clear choice.
Fill both blanks to correctly merge and return aggregated data from services.
async function fetchDataFromServices() {
const [dataA, dataB] = await Promise.all([
fetchServiceA(),
fetchServiceB()
]);
return { ...dataA[1]...dataB[2] };
}Use commas to separate spread objects inside the returned object literal for correct merging.
Fill all three blanks to implement caching in the aggregator service.
const cache = new Map();
async function fetchDataFromServices() {
if (cache.has([1])) {
return cache.get([2]);
}
const data = await Promise.all([
fetchServiceA(),
fetchServiceB()
]);
cache.set([3], data);
return data;
}Using the same cache key 'cacheKey' consistently ensures correct caching and retrieval.