js中Promise简单使用

js中Promise简单使用,第1张

<script >         //定义一个返回Promise对象方法      const getlist=((type)=>{                  //resolve表示完成 *** 作         //reject异常 *** 作         return  new Promise((resolve,reject)=>{

 

            // throw{             //             name:‘typeErr‘,             //             message:‘you should enter a number‘             //         }           if(type==1){           resolve(‘错误啦。。。‘)}           else{           reject(‘对啦。。。‘)         } })      })         window.onload=( ()=>{                          //noawait();              yawait();         })                  //不用await         const noawait=(()=>{            getlist(2).then((result)=>{             console.log(‘result‘);             console.log(result);            }).catch((err)=>{             console.log(‘err me‘);              console.log(err);            })         })       //使用await方式接收         const yawait=(async ()=>{           try {           await只接受resolve返回的内容             let data =await getlist(1);            console.log(data);           } catch (error) {            reject返回的需要在catch里进行捕捉               console.log(error);           }            

 

        })     </script>

js中Promise简单使用

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/1006881.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存