当前位置: 首页 > news >正文

政府门户网站制度建设情况广州专做优化的科技公司

政府门户网站制度建设情况,广州专做优化的科技公司,做网站公司汉狮价格,泰安网站优化背景 背景就是遇到了一个比较烦人的模块,里面的涉及到了大量的async 和 awiat。发现大多人对这个语法糖一知半解,然后大量的滥用,整理一下 async 前置知识: Promise.resolve(foo) new Promise(resolve > resolve(foo)…

背景

背景就是遇到了一个比较烦人的模块,里面的涉及到了大量的async 和 awiat。发现大多人对这个语法糖一知半解,然后大量的滥用,整理一下

async

前置知识:

 Promise.resolve('foo)   ===    new Promise(resolve => resolve('foo'))Promise.reject('foo)    ===  new Promise((resolve, reject) => reject('出错了'))

1、async修饰的函数返回一个promise

async function myName() {let result = await Promise.resolve("hello")let result1 =  await Promise.resolve("hello1")console.log(result)console.log(result1)
}
myName().then(e => console.log(e))
//hello
//hello1
//undefined (函数没有返回任何的值所以是undefined)---------------------
async function myName() {let result = await Promise.resolve("hello")let result1 =  await Promise.resolve("hello1")return ({result,result1})
}
myName().then(e => console.log(e))
// { result: 'hello', result1: 'hello1' }

2、注意以下用法,以下用法在项目中使用是极多的

i:以下的这种写法就很好理解了,没问题的

function timeout(ms) {return new Promise((resolve) => {setTimeout(resolve, ms);});
}
async function asyncPrint(value, ms) {await timeout(ms);console.log(value);
}asyncPrint('hello world', 50)
// hello world

ii:因为async返回一个promise,所以下述写法完全等同于i的写法

async function timeout(ms) {await new Promise((resolve) => {setTimeout(resolve, ms);});
}
async function asyncPrint(value, ms) {await timeout(ms);console.log(value);
}
asyncPrint('hello world', 50)
// hello world

在这里插入图片描述

async function timeout(ms) {await new Promise((resolve) => {setTimeout(resolve, ms);});console.log(8888)
}
async function asyncPrint(value, ms) {let res =  timeout(ms)console.log(res) console.log(value);
}
asyncPrint('hello world', 50)
// Promise { <pending> }
// hello world
// 8888
async function timeout(ms) {await new Promise((resolve) => {setTimeout(resolve, ms);});console.log(8888)
}async function asyncPrint(value, ms) {let res =  timeout(ms)console.log(res) await timeout(ms);console.log(value);
}
asyncPrint('hello world', 50)
//Promise { <pending> }
// 8888
// 8888
// hello world

await

await修饰异步,在async中使用,当promise是resolve时接着往下走

1、awiat(直接用),只能接收resolve返回的参数

async function myName() {let result = await Promise.resolve("hello")let result1 = await Promise.resolve("hello1")console.log(result)console.log(result1)
}
myName()
// hello
// hello1
async function myName1() {let result = await Promise.reject("hello")console.log(result)
}
myName1()
// 报错了
------------
async function myName1() {let result = await Promise.reject("hello")console.log(111111111111111)console.log(result)
}
myName1()
// 报错了(console都没走)

2、搭配 try catch 可以用 catch捕捉到reject的错误

async function myName2() {try {let result = await Promise.reject("hello")console.log(result)} catch (error) {console.log('出错了',error)}
}
myName2()
// 出错了 hello

3、try catch ,try内之要有一个promise reject,那么后续的就都不会进行了,直接将第一个reject给catch给出去了

async function myName2() {try {let result = await Promise.reject("hello")console.log(result)let result1 = await Promise.resolve("hello word")console.log(result1)} catch (error) {console.log('出错了',error)}
}
myName2()
// 出错了 hello
----------------------------------------
// 下方demo为了证明,报错后没有再往后走
async function myName2() {try {await Promise.reject("hello")console.log('走不走')let result1 = await Promise.resolve("hello word")console.log(result1)} catch (error) {console.log('出错了',error)}
}
myName2()
// 出错了 hello

   function name() {try {throw new Error('出错了');console.log(1111);} catch (e) {console.log(e);}}

如果抛出错误 throw new Error 就不在往下走了,不会执行执行打印,走到了catch

    async onSubmit() {this.lastClickEvent = this.CLICK_EVENT.ADD;try {await this.commonAdd();this.$message({type: 'success',message: this.$t('msg_success')});if (this.isClient) {this.SynchronizeResourceUpdate();}if (!this.accessGuide) {this.back();}} catch (error) {console.log(error);}},

commonAdd如果有throw new Error也就不往下走了,try catch被中断

await 结果接收

await将结果赋值,只能在正确(resolve)的时候处理

async function ll() {let w = await Promise.resolve('出错了');console.log(w);let y = await Promise.resolve('hello');console.log(y);
}
ll();
// 出错了
// hello
async function ll() {let w = await Promise.reject('出错了');console.log(w);let y = await Promise.reject('hello');console.log(y);
}
ll(); 
// Promise {<rejected>: "出错了"}
http://www.mmbaike.com/news/83657.html

相关文章:

  • 义乌网站建设方案详细百度移动
  • 如何为公司做网站seo综合查询怎么关闭
  • 网站开发pythonseo的含义
  • 广告发布属于什么服务seo推广薪资
  • 行业网站建设教程一键制作免费网站的app
  • 北京高端网站建设优势网站怎么提升关键词排名
  • 网站建设学校培训学校百度怎么注册自己的店铺
  • 邹平县建设局网站宁波网站推广营销
  • 食品网站建设的照片网站怎么推广效果好一点呢
  • 房山手机网站建设河南网站建设
  • 云主机建站免费技能培训网
  • 公司简介50字附子seo教程
  • 无锡网站推企业宣传推广怎么做
  • 网站开发前端课程图片搜索图片识别
  • 科技网站设计公司关键词优化搜索排名
  • 银行党风廉政建设考试网站郑州seo网络营销
  • 网站开发的标准流程小程序推广平台
  • 制作网站软件网页设计代做
  • 网站建设方案书组网方案河南品牌网络推广外包
  • 最好的微网站建设价格企业管理培训班哪个好
  • 车公庙网站建设百度建站官网
  • 丽江市企业网站搜狗推广效果好吗
  • 宁波网站建设模板下载乌鲁木齐seo
  • 网站开发维护专员岗位职责宣城网站seo
  • 东丽手机网站建设百度搜索排名购买
  • 优秀网站建设关键词筛选工具
  • 专业的网站建设专业平台seo l
  • 做户外照明有哪些网站职业培训机构需要什么资质
  • 用狐狸做logo的网站营销网站建设选择
  • 万维网 网站 主页 网页室内设计网站