美文网首页
newpromise与await的并行用法

newpromise与await的并行用法

作者: 苏本的书柜 | 来源:发表于2019-04-18 21:37 被阅读0次

await串行用法

let getAllInfo= async ()=> {
             // 获取个人信息
            let result = await this.pullInfo();
            // 获取新闻信息
            let result1 = await this.pullList();
            // 这里会等result和result1运行完毕
            // dom渲染结束之后,然后转换为图片以及获取width
            this.$nextTick(()=>{
                    //修改关注按钮的值
                        document.querySelector('.suball').style.display ='none';   
                        this.canvasImage()
                        this.getwidth()
                    })            
        }
        getAllInfo();
 pullInfo(){
        if (!this.userId) {
            return 
        }
        return  this.$get(`/app/news/home`,{
             uid:this.userId
             }).then(o => {
              let personInfo=  o.userBusinessCard
              for (const key in personInfo) {
                //    如果为null,就直接为null  假如为null,需要处理
                 if(personInfo[key]=="null"){
                     personInfo[key]=''
                 }              
             }
              this.personInfo= personInfo
              console.log(o);
            }).catch((error) => {
                console.log(error);
            })
        },
        pullList(){
         if (!this.newId) {
             return    
         }
        return this.$get(`/app/news/list`,{
             ids:this.newId
             }).then(o => {
              this.list=o
             
            }).catch((error) => {
                console.log(error);
            })
        },

promose 串行用法

 pullInfo(){
        if (!this.userId) {
            return 
        }
        return  this.$get(`/app/news/home`,{
             uid:this.userId
             }).then(o => {
              let personInfo=  o.userBusinessCard
              for (const key in personInfo) {
                //    如果为null,就直接为null  假如为null,需要处理
                 if(personInfo[key]=="null"){
                     personInfo[key]=''
                 }              
             }
              this.personInfo= personInfo
              console.log(o);
            }).catch((error) => {
                console.log(error);
            })
        },
        pullList(){
         if (!this.newId) {
             return    
         }
        return this.$get(`/app/news/list`,{
             ids:this.newId
             }).then(o => {
              this.list=o
             
            }).catch((error) => {
                console.log(error);
            })
        },
// 在执行之后,然后再转换图片,用promise方法,出现一点小问题
        let p1 = new Promise( (resolve, reject) =>{
              // 获取信息
               this.pullInfo()       
                // setTimeout(resolve, 500, 'P1');
            });
        let p2 = new Promise( (resolve, reject) => {
             // 获取列表
               this.pullList()
                
                // setTimeout(resolve, 600, 'P2');
            });
        // 同时执行p1和p2,并在它们都完成后执行then:
        Promise.all([p1, p2]).then( (results) => {
            debugger
            // dom渲染结束之后,然后转换为图片以及获取width
            this.$nextTick(()=>{
                        this.canvasImage()
                        this.getwidth()
                    })
            });

相关文章

网友评论

      本文标题:newpromise与await的并行用法

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