time 6m
return使得之后的next变成undefined,直接就true了,终结了遍历器的迭代方式
因为终结了,所以之后next返回undefined

  1. function * get(){
  2. yield 1;
  3. yield 2;
  4. yield 3;
  5. }
  6. let g=get();
  7. console.log(g.next());
  8. console.log(g.return());
  9. console.log(g.next());
  10. console.log(g.next());
  11. console.log(g.next());

image.png

  1. function * get(){
  2. yield 1;
  3. // return 10;
  4. yield 2;
  5. yield 3;
  6. }
  7. let g=get();
  8. console.log(g.next());
  9. /*return 10变成10了
  10. * 与在get中return 10一样*/
  11. console.log(g.return(10));
  12. console.log(g.next());
  13. console.log(g.next());
  14. console.log(g.next());

image.png

throw

time 15:08
一般不会直接throw,因为直接终止程序运行了

  1. /*报错下面的console代码不会执行*/
  2. throw new Error('1');
  3. console.log(1);

try与throw

一般throw与try混用,一块用,不会终止程序
time 16m7s

  1. try {
  2. // console.log(a);//25:ReferenceError: a is not defined
  3. // throw (new Error(a));//25:ReferenceError: a is not defined
  4. throw (new Error('a is not defined1'));
  5. //25:Error: a is not defined1
  6. } catch (e) {
  7. console.log('25:' + e);
  8. }

异步与try catch

try不能捕获异步的异常

time 20m06s
try并不能捕获异步的异常
try catch本质上是同步代码,异步代码在回调队列中注册相应的函数,当事件发生的时候,会把事件放到相应的回调当中,然后再放到主进程中,放到主进程时,try catch已经执行完毕了
try在生成器函数中的区间

  1. try {
  2. /* setTimeout(function () {
  3. console.log(a)
  4. })*/
  5. console.log(a)
  6. } catch (e) {
  7. console.log(e);
  8. }

image.png

  1. try {
  2. setTimeout(function () {
  3. console.log(a)
  4. })
  5. // console.log(a)
  6. } catch (e) {
  7. console.log(e);
  8. }

image.png

next与try

time 24m33s
此时正常运行,打印,不会走try

  1. var g = function* () {
  2. try {
  3. yield;
  4. } catch (e) {
  5. console.log('生成器内部异常:' + e);
  6. }
  7. console.log(1);
  8. }
  9. var i = g();
  10. console.log(i.next());
  11. console.log('------')
  12. console.log(i.next());

time 25m27s
不会捕获异常,因为next没有运行,在yield暂停了,try catch还没有运行

  1. var g = function* () {
  2. try {
  3. yield;
  4. } catch (e) {
  5. console.log('生成器内部异常:' + e);
  6. }
  7. console.log(1);
  8. }
  9. var i = g();
  10. /*Uncaught a*/
  11. console.log(i.throw('a'));
  12. console.log('------');
  13. console.log(i.next());
  14. console.log('------');
  15. console.log(i.next());

image.png

time 26m55s
抛出异常,通过i.throw(‘a’),但异常的捕获方式需要通过try catch来

  1. var g = function* () {
  2. try {
  3. yield;
  4. } catch (e) {
  5. console.log('生成器内部异常:' + e);
  6. }
  7. console.log(1);
  8. }
  9. var i = g();
  10. console.log(i.next());
  11. console.log('------');
  12. console.log(i.throw('a'));
  13. console.log('------');
  14. console.log(i.next());

image.png

两次throw

time 34m04s
两次throw,只捕获一次,b是正常报错,没有捕获到

  1. var g = function* () {
  2. try {
  3. yield;
  4. } catch (e) {
  5. console.log('生成器内部异常:' + e);
  6. }
  7. console.log(1);
  8. }
  9. var i = g();
  10. console.log(i.next());
  11. console.log('------');
  12. console.log(i.throw('a'));
  13. console.log('------');
  14. console.log(i.throw('b'));
  15. console.log('------');
  16. console.log(i.next());

image.png

throw也可以用于迭代

time37m
throw时,发生迭代了,value的值变成了3,throw抛出错误,迭代两种功能

  1. var g = function* () {
  2. yield 1;
  3. try {
  4. yield 2;
  5. } catch (e) {
  6. console.log('生成器内部异常:' + e);
  7. }
  8. yield 3;
  9. console.log(1);
  10. }
  11. var i = g();
  12. console.log(i.next());
  13. console.log('------');
  14. console.log(i.next());
  15. console.log('------');
  16. console.log(i.throw('a'));
  17. console.log('------');
  18. console.log(i.next());

image.png

time 39m
第一次运行到yield console.log(1);暂停,try catch没有动,程序暂停
throw,程序继续运行,运行try catch,打印e,运行到 yield console.log(2);程序暂停
next程序继续运行,运行到 yield console.log(3);,程序暂停

  1. function * gen(){
  2. try {
  3. yield console.log(1);
  4. }catch (e) {
  5. console.log(e)
  6. }
  7. yield console.log(2);
  8. yield console.log(3);
  9. }
  10. let i=gen();
  11. i.next();
  12. console.log('------');
  13. i.throw('k');
  14. console.log('------');
  15. i.next();

image.png

  1. function * gen(){
  2. try {
  3. yield console.log(1);
  4. }catch (e) {
  5. console.log(e)
  6. }
  7. yield console.log(2);
  8. yield console.log(3);
  9. console.log('n')
  10. }
  11. let i=gen();
  12. i.next();
  13. console.log('------');
  14. i.throw('k');
  15. console.log('------');
  16. i.next();
  17. console.log('------');
  18. i.next();

image.png

综合运用

time 44m34s
上节课的代码

  1. let fs = require('fs');
  2. let util = require('util');
  3. let co = require('co');
  4. let readFile = util.promisify(fs.readFile);
  5. function* read() {
  6. let value1 = yield readFile('./name.txt', 'utf-8');
  7. let value2 = yield readFile(value1, 'utf-8');
  8. let value3 = yield readFile(value2, 'utf-8');
  9. return value3;
  10. }
  11. let promise = co(read());
  12. promise.then(val=>{
  13. console.log(val);
  14. })

异常捕获

time 48m27s

  1. let fs = require('fs');
  2. let util = require('util');
  3. let co = require('co');
  4. let readFile = util.promisify(fs.readFile);
  5. /*try在生成器函数中可以捕获到异常*/
  6. function* read() {
  7. try {
  8. /*改成tx,try可以捕获到,这里面yield里面必须跟着promise对象,通过promisify转的*/
  9. let value1 = yield readFile('./name.tx', 'utf-8');
  10. let value2 = yield readFile(value1, 'utf-8');
  11. let value3 = yield readFile(value2, 'utf-8');
  12. return value3;
  13. } catch (e) {
  14. console.log('91:'+e);
  15. }
  16. }
  17. let promise = co(read());
  18. promise.then(val => {
  19. console.log(val);
  20. })
  21. /*91:Error: ENOENT: no such file or directory, open */

async函数

time 55m41s

  1. let fs = require('fs');
  2. let util = require('util');
  3. let co = require('co');
  4. let readFile = util.promisify(fs.readFile);
  5. /*try在生成器函数中可以捕获到异常*/
  6. async function read() {
  7. /* try {
  8. /!*改成tx,try可以捕获到*!/
  9. let value1 = yield readFile('./name.tx', 'utf-8');
  10. let value2 = yield readFile(value1, 'utf-8');
  11. let value3 = yield readFile(value2, 'utf-8');
  12. return value3;
  13. } catch (e) {
  14. console.log('91:' + e);
  15. }*/
  16. /*await其实是将co封装了里面,不需要co了,本质上就是个语法糖*/
  17. let value1 = await readFile('./name.txt', 'utf-8');
  18. let value2 = await readFile(value1, 'utf-8');
  19. let value3 = await readFile(value2, 'utf-8');
  20. return value3;
  21. }
  22. // let promise = co(read());
  23. let promise = read();
  24. promise.then(val => {
  25. console.log(val);
  26. })
  27. /*99*/
  28. //time 1h00m55s
  29. /*1.内置执行器;co
  30. * 2.更好的语义;*语义化不明确
  31. * 3.更广的实用性,await不用必须跟着promise对象,就普通语法就行了
  32. * 4返回值一定是promise;*/

返回值

time 1h7m13s

  1. /*try在生成器函数中可以捕获到异常*/
  2. async function read() {
  3. /*await其实是将co封装了里面,不需要co了,本质上就是个语法糖*/
  4. let value1 = await readFile('./name.txt', 'utf-8');
  5. /* let value2 = await readFile(value1, 'utf-8');
  6. let value3 = await readFile(value2, 'utf-8');*/
  7. console.log('207:'+value1);
  8. /*value1的值是./number.txt,return时通过Promise.resole(value1)再封装*/
  9. return value1;
  10. }
  11. // let promise = co(read());
  12. let promise = read();
  13. promise.then(val => {
  14. console.log('216:'+val);
  15. })
  16. /*207:./number.txt
  17. 216:./number.txt*/

async与错误

time 1h12m54s
报错,捕获错误可以通过promise来捕获

  1. let fs = require('fs');
  2. let util = require('util');
  3. let co = require('co');
  4. let readFile = util.promisify(fs.readFile);
  5. /*try在生成器函数中可以捕获到异常*/
  6. async function read() {
  7. /*await其实是将co封装了里面,不需要co了,本质上就是个语法糖*/
  8. let value1 = await readFile('./name.txt', 'utf-8');
  9. /* let value2 = await readFile(value1, 'utf-8');
  10. let value3 = await readFile(value2, 'utf-8');*/
  11. /*207代码行数,这行代码在第几行*/
  12. console.log('207:'+value1);
  13. console.log(a);
  14. console.log('211:'+value1);
  15. /*value1的值是./number.txt,return时通过Promise.resole(value1)再封装*/
  16. return value1;
  17. }
  18. // let promise = co(read());
  19. let promise = read();
  20. promise.then(val => {
  21. console.log('216:'+val);
  22. },(e)=>{
  23. console.log('220:'+e);
  24. /*220:ReferenceError: a is not defined*/
  25. })

也可以通过try catch

promise.all

time 1h24m

  1. let fs = require('fs');
  2. let util = require('util');
  3. let co = require('co');
  4. let readFile = util.promisify(fs.readFile);
  5. let f = Promise.all([
  6. readFile('./name.txt', 'utf-8'),
  7. readFile('./number.txt', 'utf-8'),
  8. readFile('./score.txt', 'utf-8'),
  9. ])
  10. f.then(value => {
  11. console.log(value);
  12. },err=>{
  13. console.log(err);
  14. })

time 1h31m
通过set使得突破promise.all只要有一个出错,就只会打印那一个报错的限制
可以就弄一个时间戳,什么都不写,先概览,之后再写
image.png

课程

time 1h44m
image.png

有效学习
内容多 怎么学 学了会忘
做笔记,复习笔记

录播很重要,复习,看30分钟就行了

作息时间很重要