throw在js生成器中的用法

说明

1、生成器函数的外部可以向throw方法传达参数,该参数被catch语句捕获。

2、不传达参数,catch语句捕获为undefined,catch语句捕获后恢复生成器的执行,具有IteratorResult。

实例

constcaughtInsideCounter=(function*(){
letc=0;
while(true){
try{
yield++c;
}catch(e){
console.log(e);
}
}
})();

caughtInsideCounter.next();//{value:1,done:false}
caughtIndedeCounter.throw(newError('Anerroroccurred!'));
//输出Anerroroccurred!
//{value:2,done:false}

以上就是throw在js生成器中的用法,希望对大家有所帮助。更多Javascript学习指路:Javascript

原文来自:https://www.py.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享