快速学习ES6新特性-Generator函数
- 2020 年 2 月 14 日
- 笔记
1.10、Generator函数
Generator 函数是 ES6 提供的 一种异步编程解决方案,语法行为与传统函数完全不同 。
Generator函数有两个特征: 一是 function命令与函数名 之间有一个星号: 二是 函数体内部使用 yield吾句定义不同的内部状态。
用法:

可以看到,通过hello()返回的h对象,每调用一次next()方法返回一个对象,该对象包含了value值和done状态。直到 遇到return关键字或者函数执行完毕,这个时候返回的状态为ture,表示已经执行结束了。
1.10.1、for…of循环
通过for…of可以循环遍历Generator函数返回的迭代器。用法:
