Last active
March 9, 2019 23:44
-
-
Save tranduclinh2067/8fc40d6a7ff8f254d988be75d2c09b16 to your computer and use it in GitHub Desktop.
Muốn tạm dừng 1 dòng code nào đó để làm 1 việc khác, thực thi lại thông qua nhiều gần gọi giống nhau thông qua .next().
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generator function là một function, có khả năng tạm ngưng thực thi trước khi hàm kết thúc, và có thể tiếp tục chạy ở 1 thời điểm khác. | |
// --------- Ví dụ đơn giản | |
function* Op(name) { | |
yield name * 5; // Thực thi lần thứ 1 | |
yield name; // Thực thi lần thứ 2 | |
} | |
const call = Op(4); | |
console.log(call.next().value); // Lần gọi thứ 1 | |
console.log(call.next().value); // Lần gọi thứ 2 | |
// 20 | |
// 4 | |
// --------- Ví dụ lấy từng index có trong ARRAY thông qua (yield*) | |
function* Op_2() { | |
yield*['hello', 'world'] | |
} | |
var call_2 = Op_2() | |
console.log(s.next().value) // Lần gọi thứ 1 | |
console.log(s.next().value) // Lần gọi thứ 2 | |
// hello | |
// world |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment