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

北京网站建设公司房山华网网站推广方案范例

北京网站建设公司房山华网,网站推广方案范例,毕业答辩问题怎么做的这个网站,娄底市网站建设1. 什么是 async/await async/await 是 ES8(ECMAScript 2017)引入的新语法,用来简化 Promise 异步操作。在 async/await 出 现之前,开发者只能通过链式 .then() 的方式处理 Promise 异步操作。示例代码如下: import …

1. 什么是 async/await

async/await ES8(ECMAScript 2017)引入的新语法,用来简化 Promise 异步操作。在 async/await 出 现之前,开发者只能通过链式 .then() 的方式处理 Promise 异步操作。示例代码如下:

import thenFs from 'then-fs'thenFs.readFile("./files/1.txt",'utf8') // 返回值是 promise 的实例对象。
.then(r1=>{   // 通过 .then 为第一个 Promise 实例指定成功之后的回调函数。console.log(r1)return thenFs.readFile("./files/2.txt",'utf8')  // 在第一个 .then 中返回一个新的 promise 的实例对象。
})
.then(r2=>{ // 继续调用 .then 为上一个 .then 的返回值(新的 Promise 实例) 指定成功之后的回调函数。console.log(r2)return thenFs.readFile("./files/3.txt",'utf8')  // 在第二个 .then 中返回一个新的 promise 的实例对象。
})
.then(r3=>{ // 继续调用 .then 为上一个 .then 的返回值(新的 Promise 实例) 指定成功之后的回调函数。console.log(r3)
})// 运行结果:
// txt file 1
// txt file 2
// txt file 3
  • .then 链式调用的优点: 解决了回调地狱的问题
  • .then 链式调用的缺点: 代码冗余、阅读性差、 不易理解 

 2. async/await 的基本使用

import thenFs from "then-fs";//按照顺序读取 1,2,3文件的内容
async function getAllFile(){const r1 = await thenFs.readFile("./files/1.txt",'utf8')// 当在 thenFs.readFile()方法前面添加 await 关键字时,返回的不是一个 Promise 实例了,而是文件的内容。console.log(r1)const r2 = await thenFs.readFile("./files/2.txt",'utf8')console.log(r2)const r3 = await thenFs.readFile("./files/3.txt",'utf8')console.log(r3)
}
//调用方法
getAllFile();// 运行结果:
// txt file 1
// txt file 2
// txt file 3

3. async/await 的使用注意事项

  1. 如果在 function 中使用了 await,则 function 必须被 async 修饰
  2. 在 async 方法中,第一个 await 之前的代码会同步执行,await 之后的代码会异步执行
import thenFs from "then-fs";console.log("A")
//按照顺序读取 1,2,3文件的内容
async function getAllFile(){console.log("B")const r1 = await thenFs.readFile("./files/1.txt",'utf8')const r2 = await thenFs.readFile("./files/2.txt",'utf8')const r3 = await thenFs.readFile("./files/3.txt",'utf8')console.log(r1,r2,r3)console.log("D")
}//调用方法
getAllFile();
console.log("C")// 运行结果:
// A
// B
// C
// txt file 1
// txt file 2
// txt file 3
// D
http://www.shuangfujiaoyu.com/news/8606.html

相关文章:

  • 为什么要创建网站子目录上海热点新闻
  • 上海营销型网站建设平台沈阳seo搜索引擎
  • java做电影广告网站360广告投放平台
  • notepad wordpress大连网站优化
  • 亚马逊产品开发流程南京广告宣传公司seo
  • wordpress主题谷歌字体大小泰州seo排名扣费
  • 本机建的网站打开却很慢雷神代刷推广网站
  • 郑州教育网站建设百度百科创建
  • 辽宁企业网站建设公司开户推广竞价开户
  • 网站基础模板搜索引擎推广的费用
  • 免费加盟游戏代理外贸seo网站建设
  • 江宁区财政局网站开发区分局百度关键词优化送网站
  • 佛山企业网站建设流程营销策划方案包括哪些内容
  • 抖音官网链接网站怎么做站长工具seo综合查询权重
  • 有的网站显示正在建设中seo引擎优化是做什么的
  • 制作宣传网站有哪些知乎小说推广对接平台
  • 沈阳博士男科医院好吗长沙专业seo优化公司
  • 深圳房地产网站开发网站建设的基本流程
  • 用旧手机做网站促销策略
  • 有哪些网站可以做海报可以引流推广的app
  • 肇庆网站制作软件外贸网站优化推广
  • 模板网站可以做备案吗网络推广策划
  • 西苑做网站公司关键词排名公司
  • 化工类网站模板培训机构咨询
  • 做网站准备什么seo网络推广案例
  • 上海网站开发报价网络营销活动方案
  • 电子商务网站建设一般流程外贸网站推广公司
  • 网站做优化公司营销咨询师
  • 企业网站报价方案模板下载北京昨天出啥大事了
  • 聊城网站建设哪个好些免费发广告的网站大全