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 fetchUserAndOrders to chain the promises and return { user, orders }
  2. Export fetchUserAndOrders

Please set the playground first

Loading "Chaining Promises"
Loading "Chaining Promises"
Login to get access to the exclusive discord channel.
Loading Discord Posts