Sobes.tech
Junior — Senior

Determining the flight path between two points

livecode

Task condition

Implement a function that takes a start point, an end point, and an asynchronous method fetchFlights, which returns a list of available destinations from a given city. The function should return a Promise.resolve with an array of points forming the route, or a Promise.reject with an error containing the text "No way".

// method that returns a list of direct flights from a city
function fetchFlights(from: string): Promise<string[]>;

// Example route graph: {A: [B, D], B: [C, N, Z], D: [E, F], F: [S]}

findPath('A', 'N', fetchFlights) // Promise.resolve(['A', 'B', 'N'])
findPath('A', 'S', fetchFlights) // Promise.resolve(['A', 'D', 'F', 'S'])
findPath('B', 'S', fetchFlights) // Promise.reject(new Error('No way'))

// implementation of the composite flight search function
function findPath(start, end, fetchFlights) {
    // your code here
}