Skip to content

Commit d77131a

Browse files
committed
refactor: simplify function syntax in test.js and enhance README with async JavaScript concepts
1 parent 88fdfc4 commit d77131a

2 files changed

Lines changed: 98 additions & 4 deletions

File tree

epicshop/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
1717
function captureOutput() {
1818
const output = []
1919
return {
20-
write: (chunk, streamType) => {
20+
write(chunk, streamType) {
2121
output.push({ chunk: chunk.toString(), streamType })
2222
},
23-
replay: () => {
23+
replay() {
2424
for (const { chunk, streamType } of output) {
2525
if (streamType === 'stderr') {
2626
process.stderr.write(chunk)
@@ -105,7 +105,7 @@ async function main() {
105105
message: 'Select apps to test:',
106106
choices: ['All', ...choices],
107107
multiple: true,
108-
suggest: (input, choices) => {
108+
suggest(input, choices) {
109109
return matchSorter(choices, input, { keys: ['name'] })
110110
},
111111
})

exercises/01.promises/README.mdx

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,106 @@
11
# Promises
22

3+
## Async JavaScript Primer
4+
5+
JavaScript runs your code on a single main thread. If a task takes time (like
6+
waiting for a network response, reading a file, or a timer), the language uses
7+
asynchronous APIs to keep the main thread free so the app can stay responsive.
8+
9+
In practice, this means:
10+
11+
- **Synchronous code** runs top to bottom and blocks until it finishes.
12+
- **Asynchronous code** starts work now and delivers results later.
13+
14+
The call stack tracks which functions are currently running. Synchronous work
15+
stays on the call stack until it completes. Asynchronous work registers a
16+
callback, returns immediately, and the callback only runs later when the call
17+
stack is clear.
18+
19+
If you want a visual to explore how promise chains behave over time, check out
20+
the Promisees playground at https://bevacqua.github.io/promisees/. It only runs
21+
JavaScript (not TypeScript), so paste the JavaScript version of the example
22+
below.
23+
24+
```ts
25+
// Synchronous: runs in order, one step at a time.
26+
console.log('A')
27+
console.log('B')
28+
console.log('C')
29+
```
30+
31+
```ts
32+
// Asynchronous: timer finishes later.
33+
console.log('Start')
34+
setTimeout(() => console.log('Later'), 0)
35+
console.log('End')
36+
// Output:
37+
// Start
38+
// End
39+
// Later
40+
```
41+
42+
```ts
43+
// A promise represents "later".
44+
function getNumber() {
45+
return new Promise<number>((resolve) => {
46+
setTimeout(() => resolve(42), 100)
47+
})
48+
}
49+
50+
getNumber().then((value) => console.log('Value:', value))
51+
```
52+
53+
```ts
54+
// Promises chain, so each step waits for the previous one.
55+
function fetchUser() {
56+
return Promise.resolve({ id: 1, name: 'Alice' })
57+
}
58+
59+
fetchUser()
60+
.then((user) => user.name)
61+
.then((name) => name.toUpperCase())
62+
.then((upper) => console.log(upper))
63+
```
64+
65+
```js
66+
// A longer chain you can tweak and observe over time.
67+
function getDelayedNumber(label, value, delay) {
68+
return new Promise((resolve) => {
69+
setTimeout(() => {
70+
console.log(`${label}: ${value}`)
71+
resolve(value)
72+
}, delay)
73+
})
74+
}
75+
76+
getDelayedNumber('first', 2, 150)
77+
.then((value) => getDelayedNumber('double', value * 2, 300))
78+
.then((value) => getDelayedNumber('plus five', value + 5, 200))
79+
.then((value) => getDelayedNumber('square', value * value, 250))
80+
.then((value) => {
81+
if (value > 100) {
82+
throw new Error('Value is too large')
83+
}
84+
return getDelayedNumber('half', value / 2, 150)
85+
})
86+
.then((value) => console.log('final value', value))
87+
.catch((error) => console.error('chain failed', error))
88+
```
89+
90+
Try pasting that chain into the Promisees playground to see how each step
91+
waits, resolves, or throws as you tweak the delays and values.
92+
93+
Promises are the standard way to represent that "later" value. You can attach
94+
handlers for success and failure, and you can combine multiple async operations
95+
without getting lost in nested callbacks.
96+
397
Promises are the foundation of asynchronous programming in JavaScript and
498
TypeScript. They represent a value that will be available in the future—either
599
a successful result or a failure.
6100

7101
```ts
8102
// A Promise that resolves to a string
9-
const fetchUser = (): Promise<string> => {
103+
function fetchUser(): Promise<string> {
10104
return new Promise((resolve) => {
11105
setTimeout(() => resolve('Alice'), 1000)
12106
})

0 commit comments

Comments
 (0)