Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise #221

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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`.
Kết quả là: `1`.

The second call to `resolve` is ignored, because only the first call of `reject/resolve` is taken into account. Further calls are ignored.
Lần gọi thứ 2 đến `resolve` bị bỏ qua, vì chỉ có lần gọi đầu tiên của `reject/resolve` được xem xét. Các lần gọi tiếp theo sẽ bị bỏ qua.
8 changes: 3 additions & 5 deletions 1-js/11-async/02-promise-basics/01-re-resolve/task.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# Re-resolve một promise?

# Re-resolve a promise?


What's the output of the code below?
Đoạn code dưới đây sẽ in ra gì?

```js
let promise = new Promise(function(resolve, reject) {
let promise = new Promise(function (resolve, reject) {
resolve(1);

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

delay(3000).then(() => alert('runs after 3 seconds'));
delay(3000).then(() => alert("runs after 3 seconds"));
```

Please note that in this task `resolve` is called without arguments. We don't return any value from `delay`, just ensure the delay.
Hãy nhớ rằng trong bài này `resolve` được gọi mà không có tham số. Chúng ta không trả về bất kỳ giá trị nào từ `delay`, chỉ đảm bảo thời gian trễ.
9 changes: 4 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,13 @@
# Trì hoãn với promise

# Delay with a promise
Xây dựng bên trong một hàm `setTimeout` sử dụng callbacks. Tạo một giải pháp dựa trên promise.

The built-in function `setTimeout` uses callbacks. Create a promise-based alternative.

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:
Hàm `delay(ms)` sẽ trả về một promise. Promise đó sẽ được giải quyết sau `ms` mili giây, để chúng ta có thể thêm `.then` vào nó, như sau:

```js
function delay(ms) {
// your code
}

delay(3000).then(() => alert('runs after 3 seconds'));
delay(3000).then(() => alert("runs after 3 seconds"));
```
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@

# Animated circle with 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.
Viết lại hàm `showCircle` trong giải pháp của bài tập <info:task/animate-circle-callback> để nó trả về một promise thay vì chấp nhận một callback.

The new usage:
Cách sử dụng mới

```js
showCircle(150, 150, 100).then(div => {
div.classList.add('message-ball');
showCircle(150, 150, 100).then((div) => {
div.classList.add("message-ball");
div.append("Hello, world!");
});
```

Take the solution of the task <info:task/animate-circle-callback> as the base.
Lấy giải pháp của bài tập <info:task/animate-circle-callback> làm cơ sở.
282 changes: 140 additions & 142 deletions 1-js/11-async/02-promise-basics/article.md

Large diffs are not rendered by default.