Promise 의 정적 방법

16172 단어 js
Promise 의 정적 방법
1.Promise.all()
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script src="QF.js"></script>
    <script>
        function sendAjax1() {
     
            return new Promise((resolve, reject) => {
     
                QF.get("./a.php", {
     }, (data) => {
     
                    if (!data.error) {
     
                        resolve(data);
                    } else {
     
                        reject("        ");
                    }
                });
            });
        }

        function sendAjax2() {
     
            return new Promise((resolve, reject) => {
     
                QF.get("./b.php", {
     }, (data) => {
     
                    if (!data.error) {
     
                        resolve(data);
                    } else {
     
                        reject("        ");
                    }
                })
            });
        }

        function sendAjax3() {
     
            return new Promise((resolve, reject) => {
     
                QF.get("./c.php", {
     }, (data) => {
     
                    if (!data.error) {
     
                        resolve(data);
                    } else {
     
                        reject("        ");
                    }
                })
            })
        }

        //                             
        let p1 = sendAjax1();
        let p2 = sendAjax2();
        let p3 = sendAjax3();

        var arr = [p2, p1, p3];
        // Promise.all Promise                 Promise   
        // 1                    Promise              Promise.resolve  Promise  
        // 2 Promise.all        Promise                           
        //     :            rejected   rejected,       resolved    resolved
        //                     all                    

        let p = Promise.all(arr);
        p.then(function(arr) {
     
                console.log("    ");
                console.log(arr)
            })
            .catch(function(data) {
     
                console.log(data, "failed")
            })

2.Promise.race()
 // Promise.race  Promise                  Promise 
        // 1                    Promise             Promise.resolve  Promise  
        // 2 Promise.race        Promise                          
        //     :              
        let p = Promise.race([p1, p2, p3]);
        p.then(function(data) {
     
                console.log(data, "success")
            })
            .catch(function(data) {
     
                console.log(data, "failed")
            })

3.Promise.resolve()
 // Promise.resolve  Promise              Promise  Promise  
        // let p = Promise.resolve(1);  //1
        // let p = Promise.resolve("1");  //1
        // let p = Promise.resolve(true);  //true
        // let p = Promise.resolve(function() {})  //function(){}

        //        thenable     then     new Promise      
        // let p = Promise.resolve({
     
        //     then: function(resolve, reject) {
     
        //         console.log(31);
        //         resolve("aaa")
        //     }
        // })
        //       Promise      
        // let p1 = sendAjax1();
        // let p = Promise.resolve(p1);
        //                                        Promise  
        // p.then(function(data) {
     
        //     console.log(data)
        // })

좋은 웹페이지 즐겨찾기