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
and:
- Create
fetchUserAndOrdersto chain the promises and return{ user, orders } - Export
fetchUserAndOrders


