function getCalculatedRoutes(routes: Array<any>): Promise<any> {
    return Promise.all(
      routes.map(async (route: any) => {
        const distance = await getDistance();
        route.distance = distance;
        route.cost = calculateCost(distance);
        return route;
      })
    );
  }
  
  function calculateCost(distance: number): string {
    return (distance * 0.19).toFixed(2);
  }
  
  async function getDistance() {
    //simulating the API call
     return Math.floor(Math.random() * 10) + 1;
  }

  async function printResults() {
    const routes = await getCalculatedRoutes([{destination: "Malaga"}, {destination: "Barcelona"}]);
    document.write(JSON.stringify(routes));
  }

printResults();


View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.