File tree Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Expand file tree Collapse file tree 2 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -533,7 +533,7 @@ $ babel --optional es7.decorators
533533脚本中打开的命令如下。
534534
535535``` javascript 
536- babel .transfrom (" code"  , {optional:  [" es7.decorators"  ]})
536+ babel .transform (" code"  , {optional:  [" es7.decorators"  ]})
537537``` 
538538
539539Babel的官方网站提供一个[ 在线转码器] ( https://babeljs.io/repl/ ) ,只要勾选Experimental,就能支持Decorator的在线转码。
Original file line number Diff line number Diff line change @@ -56,7 +56,7 @@ then方法可以接受两个回调函数作为参数。第一个回调函数是P
5656
5757``` javascript 
5858function  timeout (ms ) {
59-   return  new  Promise ((resolve ) =>  {
59+   return  new  Promise ((resolve ,  reject ) =>  {
6060    setTimeout (resolve, ms, ' done'  );
6161  });
6262}
@@ -68,6 +68,26 @@ timeout(100).then((value) => {
6868
6969上面代码中,timeout方法返回一个Promise实例,表示一段时间以后才会发生的结果。过了指定的时间(ms参数)以后,Promise实例的状态变为Resolved,就会触发then方法绑定的回调函数。
7070
71+ 下面是异步加载图片的例子。
72+ 
73+ ``` javascript 
74+ function  loadImageAsync (url ) {
75+   return  new  Promise (function (resolve , reject ) {
76+     var  image =  new  Image ();
77+ 
78+     image .onload  =  function () {
79+       resolve (image);
80+     };
81+ 
82+     image .onerror  =  function () {
83+       reject (new  Error (' Could not load image at '   +  url));
84+     };
85+ 
86+     image .src  =  url;
87+   });
88+ }
89+ ``` 
90+ 
7191下面是一个用Promise对象实现的Ajax操作的例子。
7292
7393``` javascript 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments