mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-29 05:42:20 +00:00
docs(generator): edit generator
This commit is contained in:
parent
a0300bd0ed
commit
e657a675e8
@ -537,47 +537,9 @@ try {
|
||||
|
||||
上面代码中,`throw`命令抛出的错误不会影响到遍历器的状态,所以两次执行`next`方法,都取到了正确的操作。
|
||||
|
||||
这种函数体内捕获错误的机制,大大方便了对错误的处理。如果使用回调函数的写法,想要捕获多个错误,就不得不为每个函数写一个错误处理语句。
|
||||
这种函数体内捕获错误的机制,大大方便了对错误的处理。如果使用回调函数的写法,想要捕获多个错误,就不得不为每个函数内部写一个错误处理语句。现在可以只在Generator函数内部写一次`catch`语句。
|
||||
|
||||
```javascript
|
||||
foo('a', function (a) {
|
||||
if (a.error) {
|
||||
throw new Error(a.error);
|
||||
}
|
||||
|
||||
foo('b', function (b) {
|
||||
if (b.error) {
|
||||
throw new Error(b.error);
|
||||
}
|
||||
|
||||
foo('c', function (c) {
|
||||
if (c.error) {
|
||||
throw new Error(c.error);
|
||||
}
|
||||
|
||||
console.log(a, b, c);
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
使用Generator函数可以大大简化上面的代码。
|
||||
|
||||
```javascript
|
||||
function* g(){
|
||||
try {
|
||||
var a = yield foo('a');
|
||||
var b = yield foo('b');
|
||||
var c = yield foo('c');
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
console.log(a, b, c);
|
||||
}
|
||||
```
|
||||
|
||||
反过来,Generator函数内抛出的错误,也可以被函数体外的`catch`捕获。
|
||||
Generator函数体外抛出的错误,可以在函数体内捕获;反过来,Generator函数体内抛出的错误,也可以被函数体外的`catch`捕获。
|
||||
|
||||
```javascript
|
||||
function *foo() {
|
||||
|
@ -168,15 +168,18 @@ var p2 = new Promise(function (resolve, reject) {
|
||||
var p1 = new Promise(function (resolve, reject) {
|
||||
setTimeout(() => reject(new Error('fail')), 3000)
|
||||
})
|
||||
|
||||
var p2 = new Promise(function (resolve, reject) {
|
||||
setTimeout(() => resolve(p1), 1000)
|
||||
})
|
||||
p2.then(result => console.log(result))
|
||||
p2.catch(error => console.log(error))
|
||||
|
||||
p2
|
||||
.then(result => console.log(result))
|
||||
.catch(error => console.log(error))
|
||||
// Error: fail
|
||||
```
|
||||
|
||||
上面代码中,`p1`是一个Promise,3秒之后变为`rejected`。`p2`的状态由`p1`决定,1秒之后,`p2`调用`resolve`方法,但是此时`p1`的状态还没有改变,因此`p2`的状态也不会变。又过了2秒,`p1`变为`rejected`,`p2`也跟着变为`rejected`。
|
||||
上面代码中,`p1`是一个Promise,3秒之后变为`rejected`。`p2`的状态在1秒之后改变,`resolve`方法返回的是`p1`。此时,由于`p2`返回的是另一个Promise,所以后面的`then`语句都变成针对后者(`p1`)。又过了2秒,`p1`变为`rejected`,导致触发`catch`方法指定的回调函数。
|
||||
|
||||
## Promise.prototype.then()
|
||||
|
||||
|
@ -127,7 +127,9 @@ fproxy.foo // "Hello, foo"
|
||||
|
||||
**(1)get(target, propKey, receiver)**
|
||||
|
||||
拦截对象属性的读取,比如`proxy.foo`和`proxy['foo']`,返回类型不限。最后一个参数`receiver`可选,当`target`对象设置了`propKey`属性的`get`函数时,`receiver`对象会绑定`get`函数的`this`对象。
|
||||
拦截对象属性的读取,比如`proxy.foo`和`proxy['foo']`。
|
||||
|
||||
最后一个参数`receiver`是一个对象,可选,参见下面`Reflect.get`的部分。
|
||||
|
||||
**(2)set(target, propKey, value, receiver)**
|
||||
|
||||
|
@ -402,13 +402,14 @@ class MyClass {
|
||||
```javascript
|
||||
let arr1 = ['c', 'd'];
|
||||
['a', 'b'].concat(arr1, 'e') // ['a', 'b', 'c', 'd', 'e']
|
||||
arr1[Symbol.isConcatSpreadable] // undefined
|
||||
|
||||
let arr2 = ['c', 'd'];
|
||||
arr2[Symbol.isConcatSpreadable] = false;
|
||||
['a', 'b'].concat(arr2, 'e') // ['a', 'b', ['c','d'], 'e']
|
||||
```
|
||||
|
||||
上面代码说明,数组的`Symbol.isConcatSpreadable`属性默认为`true`,表示可以展开。
|
||||
上面代码说明,数组的默认行为是可以展开。`Symbol.isConcatSpreadable`属性等于`true`或`undefined`,都有这个效果。
|
||||
|
||||
类似数组的对象也可以展开,但它的`Symbol.isConcatSpreadable`属性默认为`false`,必须手动打开。
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user