mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 10:22:23 +00:00
edit decorator
This commit is contained in:
parent
04d80fcbb4
commit
6bf574aeb5
@ -533,7 +533,7 @@ $ babel --optional es7.decorators
|
||||
脚本中打开的命令如下。
|
||||
|
||||
```javascript
|
||||
babel.transfrom("code", {optional: ["es7.decorators"]})
|
||||
babel.transform("code", {optional: ["es7.decorators"]})
|
||||
```
|
||||
|
||||
Babel的官方网站提供一个[在线转码器](https://babeljs.io/repl/),只要勾选Experimental,就能支持Decorator的在线转码。
|
||||
|
@ -56,7 +56,7 @@ then方法可以接受两个回调函数作为参数。第一个回调函数是P
|
||||
|
||||
```javascript
|
||||
function timeout(ms) {
|
||||
return new Promise((resolve) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(resolve, ms, 'done');
|
||||
});
|
||||
}
|
||||
@ -68,6 +68,26 @@ timeout(100).then((value) => {
|
||||
|
||||
上面代码中,timeout方法返回一个Promise实例,表示一段时间以后才会发生的结果。过了指定的时间(ms参数)以后,Promise实例的状态变为Resolved,就会触发then方法绑定的回调函数。
|
||||
|
||||
下面是异步加载图片的例子。
|
||||
|
||||
```javascript
|
||||
function loadImageAsync(url) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var image = new Image();
|
||||
|
||||
image.onload = function() {
|
||||
resolve(image);
|
||||
};
|
||||
|
||||
image.onerror = function() {
|
||||
reject(new Error('Could not load image at ' + url));
|
||||
};
|
||||
|
||||
image.src = url;
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
下面是一个用Promise对象实现的Ajax操作的例子。
|
||||
|
||||
```javascript
|
||||
|
Loading…
x
Reference in New Issue
Block a user