美文网首页
promise小练习

promise小练习

作者: 糖醋里脊120625 | 来源:发表于2020-01-08 15:24 被阅读0次
        /*
        var p = new Promise(function(resolve, reject) {
            console.log("create a promise");
            resolve("success");
        });
        console.log("after new Promise");
        p.then(function(value) {
            console.log(value);
        });
        
        //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        Promise.resolve(11)
        .then((res) => {
            console.log(res)
            return 2
        })
        .catch((err) => {
            return 3
        })
        .then((res) => {
            console.log("res")
        })
        //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        function taskA() {
            console.log("Task A");
        }
        
        function taskB() {
            console.log("Task B");
        }
        
        function onRejected(error) {
            console.log("Catch Error: A or B", error);
        }
        
        var promise = Promise.resolve();
        promise
            .then(taskA)
            .then(taskB)
            .catch(onRejected) // 捕获前面then方法中的异常
            
        //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        let p = new Promise(function (resolve, reject) {
            resolve("蚂蚁部落");
        })
        p.then(function (val) {
            console.log(val) 
        })
        //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        let p = new Promise(function(resolve, reject) {
            console.log("蚂蚁部落一");
            resolve("蚂蚁部落二");
        })
        p.then(function(val) {
            console.log(val)
        })
        console.log("蚂蚁部落三");
        //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        setTimeout(function(){
          console.log("index");
        },0); 
        console.log("蚂蚁部落");
        //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        let p = new Promise(function(resolve, reject) {
            resolve("蚂蚁部落");
        });
        p.then(function(value) {
            console.log(value);
            return value + "前端";
        }).then(function(value) {
            console.log(value);
        });
        //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        const promises = [
            Promise.resolve('a'),
            Promise.resolve('b'),
            Promise.resolve('c'),
        ];
        Promise.all(promises)
            .then((arr) => 
                console.log(arr)
        );
        //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        function timesTwoAsync(x) {
          return new Promise(resolve => resolve(x * 3));
        }
        const arr = [1, 2, 3];
        const promiseArr = arr.map(timesTwoAsync);
        Promise.all(promiseArr)
            .then(result => {
                console.log(result)
            }
        );
        //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        const urls = [
          'http://example.com/first.txt',
          'http://example.com/second.txt',
        ];
        const promises = urls.map(
            url => downloadText(url)
        );
        
        Promise.all(promises)
        .then(result => {
                console.log(result)
            }
        );
        //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        function putDookieInAnyArray(arr) {
            return [...arr, 'dookie'];
        }
        const result = putDookieInAnyArray(['I', 'really', "don't", 'like']); 
        console.log(result)
        // ["I", "really", "don't", "like", "dookie"]
        const person = {
            name: 'Todd',
            age: 29,
        };
        const copyOfTodd = { ...person };
        console.log(copyOfTodd)
        */
    

    相关文章

      网友评论

          本文标题:promise小练习

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