• es6 promise


    promise 主要用于异步编程,返回一个promise对象,这个东西可以then,继续执行后面的,前提是promise里面没有报错执行了resolve()

    let ajax = function(){
            console.log('ajax执行的我哦')
            return new Promise(function (resolve,reject) {
                setInterval(function () {
                    resolve() //执行了这里才会执行下面的then
                },1000)
            })
        }
        ajax().then(function () {
            console.log('1秒后异步执行的我哦')
        })
    

      

        // function  loadImg(src) {
        //     return new Promise((resolve, reject)=>{
        //         let img = document.createElement('img');
        //         img.src = src
        //         img.onload=function(){
        //             resolve(img)
        //         }
        //         img.onerror=function (error) {
        //             reject(error)
        //         }
        //     })
        //
        // }
        // function showImg(imgs) {
        //     imgs.forEach(function(img){
        //         document.body.appendChild(img)
        //     })
        // }
        // //都加载成功才能放置页面,其中一个不成功 就不行
        // Promise.all([
        //     loadImg('http://img0.imgtn.bdimg.com/it/u=224467989,1207151607&fm=26&gp=0.jpg'),
        //     loadImg('http://img0.imgtn.bdimg.com/it/u=224467989,1207151607&fm=26&gp=0.jpg'),
        //     loadImg('http://img0.imgtn.bdimg.com/it/u=224467989,1207151607&fm=26&gp=0.jpg')
        // ]).then(showImg)
    Promise.race()//谁先返回就显示谁,剩下的不管了
     
  • 相关阅读:
    CSP 模拟17
    晚测7
    CSP 模拟16
    CSP 模拟14
    CSP 模拟13
    Cypress系列(67)- 环境变量设置指南
    Cypress系列(66)- 测试运行最佳实践
    Cypress系列(65)- 测试运行失败自动重试
    Cypress系列(64)- 数据驱动策略
    Cypress系列(63)- 使用 Custom Commands
  • 原文地址:https://www.cnblogs.com/joer717/p/11457697.html
Copyright © 2020-2023  润新知