promise

作者: asmuzi | 来源:发表于2018-12-21 14:39 被阅读0次
    promise
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script src="./lib/jquery.js"></script>
        <script>
            let p = new Promise((resolve, reject) => {
                $.ajax({
                    url: 'data/1.txt',
                    dataType: 'json',
                    success(arr) {
                        resolve(arr);
                    },
                    error(err) {
                        reject(err);
                    }
                });
            });
    
            p.then(arr => {
                console.log('成功了' + arr);
            }, err => {
                console.log('失败了');
            });
        </script>
    </head>
    
    <body>
    
    </body>
    
    </html>
    
    promise2
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script src="./lib/jquery.js"></script>
        <script>
            let p1 = new Promise((resolve, reject) => {
                $.ajax({
                    url: 'data/1.txt',
                    dataType: 'json',
                    success(arr) {
                        resolve(arr);
                    },
                    error(err) {
                        reject(err);
                    }
                });
            });
            let p2 = new Promise((resolve, reject) => {
                $.ajax({
                    url: 'data/2.txt',
                    dataType: 'json',
                    success(arr) {
                        resolve(arr);
                    },
                    error(err) {
                        reject(err);
                    }
                });
            });
            let p3 = new Promise((resolve, reject) => {
                $.ajax({
                    url: 'data/3.txt',
                    dataType: 'json',
                    success(arr) {
                        resolve(arr);
                    },
                    error(err) {
                        reject(err);
                    }
                });
            });
    
            Promise.all([p1, p2, p3]).then(arr => {
                let [r1, r2, r3] = arr;
    
                console.log(r1);
                console.log(r2);
                console.log(r3);
            }, err => {
                console.log("错了");
            })
            // p.then(arr => {
            //     console.log('成功了' + arr);
            // }, err => {
            //     console.log('失败了');
            // });
        </script>
    </head>
    
    <body>
    
    </body>
    
    </html>
    

    相关文章

      网友评论

          本文标题:promise

          本文链接:https://www.haomeiwen.com/subject/lgvmkqtx.html