Skip to content

Commit 9a05cff

Browse files
committed
enhance exercises: add new examples for shipment tracking and playlists, update instructions for error handling and type annotations
1 parent 4efbeb5 commit 9a05cff

20 files changed

Lines changed: 81 additions & 68 deletions

File tree

exercises/01.promises/01.problem.creation/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ type User = {
88

99
// 🐨 Create a function `fetchUser` that returns a Promise<User>
1010
// The Promise should resolve after 1 second with a user object
11-
// Use setTimeout to simulate the delay
1211

1312
// 🐨 Call fetchUser and log the result when it resolves
1413

exercises/01.promises/03.problem.rejection/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function getUserProfile(id: string): Promise<UserProfile> {
3131
return fetchUser(id).then((user) => ({ status: 'success', user }) as const)
3232
}
3333

34-
// 🐨 Update getUserProfile to handle rejections with .catch()
34+
// 🐨 Update getUserProfile to handle rejections and return an error status
3535
// 💰 return { status: 'error', message: error.message }
3636
// 💰 if error isn't an Error, use 'Unknown error'
3737

exercises/01.promises/README.mdx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,18 @@ TypeScript. They represent a value that will be available in the future—either
9999
a successful result or a failure.
100100

101101
```ts
102-
// A Promise that resolves to a string
103-
function fetchUser(): Promise<string> {
102+
type Shipment = {
103+
trackingId: string
104+
status: 'label' | 'in-transit' | 'delivered'
105+
}
106+
107+
function fetchShipment(trackingId: string): Promise<Shipment> {
104108
return new Promise((resolve) => {
105-
setTimeout(() => resolve('Alice'), 1000)
109+
setTimeout(() => resolve({ trackingId, status: 'in-transit' }), 300)
106110
})
107111
}
112+
113+
fetchShipment('shp_123').then((shipment) => console.log(shipment.status))
108114
```
109115

110116
## Why Promises Matter

exercises/02.async-await/01.problem.linear-flow/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function loadUserData() {
4848
})
4949
}
5050

51-
// 🐨 Refactor `loadUserData` to use async/await instead of .then() chains
51+
// 🐨 Refactor `loadUserData` so the async flow is linear (no nested promise chains)
5252

5353
// 🐨 Export your function so we can verify your work
5454

exercises/02.async-await/02.problem.error-handling/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ function fetchOrders(userId: string): Promise<Array<Order>> {
4545
}
4646

4747
async function loadUserData(userId: string) {
48-
// 🐨 Wrap the async operations in a try block
49-
// Add a catch block to handle errors
50-
// Optionally add a finally block for cleanup
48+
// 🐨 Handle errors for the async operations
49+
// Optionally add cleanup that always runs
5150

5251
const user = await fetchUser(userId)
5352
// console.log('User:', user)

exercises/02.async-await/03.problem.promise-types/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ type Product = {
1212
price: number
1313
}
1414

15-
// 🐨 Add explicit return type annotations to these async functions
16-
// Use Promise<User> and Promise<Array<Product>> respectively
15+
// 🐨 Add explicit Promise-based return type annotations to these async functions
1716
// 🦺 async function fetchUser(): Promise<User> { ... }
1817

1918
async function fetchUser() {

exercises/02.async-await/README.mdx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ like synchronous code. It's the preferred way to write async code in modern
55
TypeScript.
66

77
```ts
8-
// With Promises
9-
fetchUser()
10-
.then((user) => console.log(user))
11-
.catch((error) => console.error(error))
12-
13-
// With async/await
14-
try {
15-
const user = await fetchUser()
16-
console.log(user)
17-
} catch (error) {
18-
console.error(error)
8+
type Playlist = { id: string; trackIds: Array<string> }
9+
type Track = { id: string; title: string }
10+
11+
async function fetchPlaylist(id: string): Promise<Playlist> {
12+
return { id, trackIds: ['t1', 't2'] }
13+
}
14+
15+
async function fetchTracks(ids: Array<string>): Promise<Array<Track>> {
16+
return ids.map((id) => ({ id, title: `Track ${id}` }))
17+
}
18+
19+
async function loadPlaylist(id: string) {
20+
const playlist = await fetchPlaylist(id)
21+
const tracks = await fetchTracks(playlist.trackIds)
22+
return { playlist, tracks }
1923
}
2024
```
2125

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
// 🐨 Export the user and product values here
2-
// Use named exports: export const user = { ... }
1+
// 🐨 Export the user and product values from this module

exercises/03.modules/01.problem.import-export/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const product = {
2020
price: 999.99,
2121
}
2222

23-
// 🐨 Open `data.ts` and move the user and product objects there
24-
// Export them using named exports, then import them here
23+
// 🐨 Move the user and product objects into `data.ts`
24+
// Export them there and import them back here
2525

2626
displayUser(user)
2727
displayProduct(product)

exercises/03.modules/02.problem.default-vs-named/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Default vs Named Exports
22

3-
// 🐨 Import the named exports: formatCurrency and formatDate
3+
// 🐨 Bring formatCurrency and formatDate in from './utils.ts'
44
// @ts-expect-error - 💣 remove this comment when it passes
55
import { formatCurrency, formatDate } from './utils.ts'
66

7-
// 🐨 Import the default export: Formatter
7+
// 🐨 Bring Formatter in from './utils.ts' as the default import
88
// @ts-expect-error - 💣 remove this comment when it passes
99
import Formatter from './utils.ts'
1010

0 commit comments

Comments
 (0)