Chaining Promises

Chaining
πŸ‘¨β€πŸ’Ό After fetching a user, we need to fetch their orders. Let's chain these operations together using .then().
type Playlist = { id: string; name: string }
type Track = { id: string; title: string }

function fetchPlaylist(): Promise<Playlist> {
	return Promise.resolve({ id: 'pl1', name: 'Morning Mix' })
}

function fetchTracks(playlistId: string): Promise<Array<Track>> {
	return Promise.resolve([{ id: `${playlistId}-t1`, title: 'Sunrise' }])
}

fetchPlaylist()
	.then((playlist) => fetchTracks(playlist.id))
	.then((tracks) => console.log(tracks))
🐨 Open
index.ts
and:
  1. Create and export fetchUserAndOrders that:
    • Calls the provided fetchUser
    • Then calls fetchOrders with that user's id
    • Returns { user, orders }
  2. Keep using .then() chaining for this step (no async/await yet)
Completion check: await fetchUserAndOrders() returns a user with id: '1' and an orders array whose first order has userId equal to that same id.

Please set the playground first

Loading "Chaining Promises"
Loading "Chaining Promises"