Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 73 additions & 74 deletions 1-js/11-async/01-callbacks/article.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions 1-js/11-async/02-promise-basics/01-re-resolve/solution.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
The output is: `1`.
ผลลัพธ์คือ `1`

The second call to `resolve` is ignored, because only the first call of `reject/resolve` is taken into account. Further calls are ignored.
การเรียก `resolve` ครั้งที่สองถูกเพิกเฉย เพราะจะนับแค่การเรียก `reject/resolve` ครั้งแรกเท่านั้น การเรียกครั้งต่อๆ ไปจะถูกเพิกเฉยทั้งหมด
2 changes: 1 addition & 1 deletion 1-js/11-async/02-promise-basics/01-re-resolve/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Re-resolve a promise?


What's the output of the code below?
ผลลัพธ์ของโค้ดด้านล่างคืออะไร?

```js
let promise = new Promise(function(resolve, reject) {
Expand Down
4 changes: 2 additions & 2 deletions 1-js/11-async/02-promise-basics/02-delay-promise/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

delay(3000).then(() => alert('runs after 3 seconds'));
delay(3000).then(() => alert('รันหลัง 3 วินาที'));
```

Please note that in this task `resolve` is called without arguments. We don't return any value from `delay`, just ensure the delay.
สังเกตว่าในโจทย์นี้ `resolve` ถูกเรียกโดยไม่มีอาร์กิวเมนต์ เราไม่ได้คืนค่าอะไรจาก `delay` แค่แน่ใจว่ามีการหน่วงเวลาก็พอ
10 changes: 5 additions & 5 deletions 1-js/11-async/02-promise-basics/02-delay-promise/task.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

# Delay with a promise
# หน่วงเวลาด้วย promise

The built-in function `setTimeout` uses callbacks. Create a promise-based alternative.
ฟังก์ชัน `setTimeout` ในตัวใช้ callback ลองสร้างอีกแบบที่ใช้ promise แทน

The function `delay(ms)` should return a promise. That promise should resolve after `ms` milliseconds, so that we can add `.then` to it, like this:
ฟังก์ชัน `delay(ms)` ควรคืน promise โดย promise นั้นจะ resolve หลังจากผ่านไป `ms` มิลลิวินาที เพื่อให้เราต่อ `.then` ได้ แบบนี้:

```js
function delay(ms) {
// your code
// โค้ดของเรา
}

delay(3000).then(() => alert('runs after 3 seconds'));
delay(3000).then(() => alert('รันหลัง 3 วินาที'));
```
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<body>

<button onclick="go()">Click me</button>
<button onclick="go()">คลิกที่นี่</button>

<script>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

# Animated circle with promise
# วงกลมเคลื่อนไหวด้วย promise

Rewrite the `showCircle` function in the solution of the task <info:task/animate-circle-callback> so that it returns a promise instead of accepting a callback.
เขียนฟังก์ชัน `showCircle` ใน solution ของโจทย์ <info:task/animate-circle-callback> ใหม่ โดยให้คืน promise แทนที่จะรับ callback

The new usage:
การใช้งานแบบใหม่:

```js
showCircle(150, 150, 100).then(div => {
Expand All @@ -12,4 +12,4 @@ showCircle(150, 150, 100).then(div => {
});
```

Take the solution of the task <info:task/animate-circle-callback> as the base.
ให้เอา solution ของโจทย์ <info:task/animate-circle-callback> เป็นฐาน
255 changes: 128 additions & 127 deletions 1-js/11-async/02-promise-basics/article.md

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions 1-js/11-async/03-promise-chaining/01-then-vs-catch/solution.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
The short answer is: **no, they are not equal**:
คำตอบสั้นๆ คือ: **ไม่เหมือนกัน**

The difference is that if an error happens in `f1`, then it is handled by `.catch` here:
ความต่างอยู่ที่ — ถ้า `f1` โยน error ออกมา `.catch` จะจัดการ error นั้นได้ในท่อนแรก:

```js run
promise
.then(f1)
.catch(f2);
```

...But not here:
...แต่ท่อนนี้ไม่ได้:

```js run
promise
.then(f1, f2);
```

That's because an error is passed down the chain, and in the second code piece there's no chain below `f1`.
เพราะ error จะถูกส่งต่อลงไปใน chain และในท่อนที่สองไม่มีอะไรต่อใต้ `f1` เลย

In other words, `.then` passes results/errors to the next `.then/catch`. So in the first example, there's a `catch` below, and in the second one there isn't, so the error is unhandled.
พูดง่ายๆ คือ `.then` ส่งทั้งผลลัพธ์และ error ต่อไปยัง `.then/.catch` ถัดไป ท่อนแรกมี `.catch` รออยู่ด้านล่าง เลยรับ error จาก `f1` ได้ แต่ท่อนที่สองไม่มี error เลยค้างไว้โดยไม่มีใครจัดการ
6 changes: 3 additions & 3 deletions 1-js/11-async/03-promise-chaining/01-then-vs-catch/task.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Promise: then versus catch
# Promise: then กับ catch ต่างกันไหม?

Are these code fragments equal? In other words, do they behave the same way in any circumstances, for any handler functions?
โค้ดสองท่อนนี้เหมือนกันไหม? พูดง่ายๆ คือในทุกสถานการณ์ ไม่ว่า handler จะเป็นอะไร ผลลัพธ์จะออกมาเหมือนกันเสมอไหม?

```js
promise.then(f1).catch(f2);
```

Versus:
กับ:

```js
promise.then(f1, f2);
Expand Down
Loading