zoukankan      html  css  js  c++  java
  • es6 读书笔记

    //Promise使用示例
    new
    Promise(function(resolve,reject){ setTimeout(function(){ var ret = "异步1执行成功!"; console.log(ret); resolve(ret); },2000); }) .then(function(msg){ return new Promise(function(resolve,reject){ setTimeout(function(){ var ret = "异步2执行成功!"; console.log(ret); ret +="->"+msg; resolve(ret); },2000); }) }) .then(function(msg){ return new Promise(function(resolve,reject){ setTimeout(function(){ var ret = "异步3执行成功!"; console.log(ret); ret +="->"+msg; resolve(ret); },2000); }) }) .then(function(msg){ console.log(msg); })
    Promise 辅助工具
          if(!Promise.wrap){
                Promise.wrap  = function(fn){
                    return function(){
                        var args = [].slice.call(arguments);
                        return new Promise(function(resolve,reject){
                            fn.apply(null,args.concat(function(err,v){
                                if(err){
                                    reject(err);
                                }
                                else{
                                    resolve(v);
                                }
                            }));
                        });
                    };
                };
            }
            
    
            function _ajax(arg,callback){
                $.ajax({
                    url:arg.url,
                    data:arg.data,
                    type:arg.type,
                    //........
                    success:function(data, textStatus, jqXHR){
                        callback(null,data);
                    },
                    error:function(xhr){
                        callback(xhr,null);
                    }
                });
            }
    
            var request = Promise.wrap(_ajax); 
            request({
                type: "POST",
                url: "some.php",
                data: "name=John&location=Boston",
            }).then(function(msg){
                
            });

     异步迭代生成器使用示例

        function request(x,y){
            $.ajax({
                url:"test.php",
                type:'post',
                data:{
                    x:x,
                    y:y,
                },
                success:function(result,status,xhr){
                   it.next(result);
                },
                error:function(xhr){
                   it.throw(xhr.statusText);
                }
            });
        }
        
        function *main (){
            try{
                var ret  = yield request(11,13);
                console.log(ret);
            }catch(err){
                console.log(err);
            }
            
        }
    
        var it = main();
        it.next();
  • 相关阅读:
    【题解】2021.3.6 杂题记录
    【题解】雅礼集训 2017 Day7 题解
    【题解】雅礼集训 2018 Day1 题解
    【题解】「WC2018」通道 [*hard]
    【题解】「CTSC2018」暴力写挂 [*hard]
    【题解】CF1485 简要题解
    【题解】「NOI2019」机器人 [*hard]
    制表符的行为
    Error: Permission denied @ apply2files
    text-align 设置 justify 后不生效
  • 原文地址:https://www.cnblogs.com/kerryw/p/9337988.html
Copyright © 2011-2022 走看看