双接口(回调)promise cb

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <div style="background: red;" id="camera"></div>
        <script src="js/jquery-1.11.0.js"></script>
        <script>
            var camera = document.getElementById('camera');
            var captureVideo = function captureVideo(opts, cb) {
                opts = opts || {};
                
                return new Promise(function (resolve, reject) {
                    $.ajax({
                        type:"get",
                        url:"http://www.jspang.com/DemoApi/newsApi.php",
                        async:true,
                        success:function(ret){
                            resolve(ret);
                            if (typeof cb === 'function') cb(ret, null);
                        },
                        error:function(err){
                            reject(err);
                            if (typeof cb === 'function') cb(null);
                        }
                    });
                });
                
            };
            captureVideo({
            width:231,
            height:123
            },(ret,err) => {//第一次回调
                console.log(12233)
                console.log(ret);
                    //console.log(err);
                }
            ).then((res)=>{//第二次回调
                console.log(5555)
                console.log(res);
            })
        
        </script>
    </body>
</html>

 

posted @ 2018-05-14 17:23  春风得意之时  阅读(118)  评论(0编辑  收藏  举报